2012-08-01 137 views
2

我有文件上传问题。当试图发送文件到我的本地服务器时,我得到“301永久移动”作为POST的响应。上传目录中没有创建文件。以下是index.php和upload.html的代码。 $ BASE_URL点到localhost/testpageslim上传的文件:301在POST响应中

[index.php文件]

$app->get('/upload/', function() use ($app) { 
    global $base_url; 
    return $app->render('upload.html', array('base_url' => $base_url)); 
}); 

$app->post('/upload/', function() use ($app) { 
    $imagename = $_FILES['image']['name']; 
    $unique_id = md5(uniqid(rand(), true)); 
    $filetype = strrchr($imgname, '.'); 
    $new_upload = 'upload' . $unique_id . $filetype; 
    move_uploaded_file($_FILES['image']['tmp_name'], $new_upload); 
    //$the_upload = copy($_FILES['image']['tmp_name'], $new_upload); 
    //@chmod($new_upload, 0777); 
    $app->redirect('/'); 
}) 

[upload.html]

<html> 
    <body> 
    <form action="{{base_url}}/upload" method="POST"> 
     <input type="file" name="photo" value="" id="image" /> 
     <input type="submit" value="Upload image" /> 
    </form> 
    </body> 
+0

你想要发生什么?您上传文件后发送重定向:'$ app-> redirect('/');' – drew010 2012-08-01 17:57:56

+0

是的,但没有重定向。它看起来像$ app-> post('/ upload /')永远不会运行。它只会重新加载/上传页面。 – GeekDaddy 2012-08-01 18:00:23

+0

检查'move_uploaded_file'的结果,我认为'$ new_upload'的路径可能不可写或可能需要一个完整路径。那么你是否还是在问题中提到的上传时没有收到“301 Moved Permanently”响应? – drew010 2012-08-01 18:10:28

回答

0

你应该引用$ _FILES数组中的 '照片',而不是'图片'。输入标签的id字段不是以表单数据发送的,它只被浏览器使用。