我正在用PHP上传一个简单的图像,之前我已经完成了100次。这次不起作用。我已经将/ tmp和uploads目录都设置为chmod 777,将目录打包为apache,我已经验证了是否填充了$ _FILES数组:
<input type="file" name="uploadimage" id="uploadimage" />
[uploadimage] => Array(
[name] => dilbert.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpkuJyyP
[error] => 0
[size] => 731602
)
这是我的代码:
$response = array();
$response['path'] = '';
$response['success'] = false;
$response['error'] = '';
$targetDir = "../../../images/uploads/";
$filepath = $targetDir . $_FILES['uploadimage']['name'];
// here just for testing since it won't return below
$response['path'] = $filepath;
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['uploadimage']['tmp_name'])) {
if(move_uploaded_file($_FILES['uploadimage']['tmp_name'], $filepath)) {
$response['success'] = true;
$response['path'] = $targetDir . $_FILES['uploadimage']['name'];
} else {
$response['error'] = 'File could not be moved';
}
} else {
$response['error'] = 'File not found';
}
} else {
$response['error'] = 'Files array not found';
}
我遇到的错误是最里面的错误,“文件无法移动” 我在AJAX调用中返回的路径是正确的,它应该是domain.com/images/uploads/filename.jpg,它是...
我手动将图像上载到上载目录domain.com/images/uploads/1.jpg,然后在上载后再次上载该图像,由于该图像已经存在,因此它在UI中得以解析,因此知道传回的路径是正确的。
我还能尝试什么?图片未以/ tmp结尾
Apache错误
PHP Warning: move_uploaded_file(../../../images/uploads/dilbert.jpg):
failed to open stream: No such file or directory in
/var/www/webapp/application/controllers/DocumentController.php
on line 75, referer: http://192.168.1.3/document/
PHP Warning: move_uploaded_file(): Unable to move '/tmp/phpm4Ivrx' to
'../../../images/uploads/dilbert.jpg' in
/var/www/webapp/application/controllers/DocumentController.php
on line 75, referer: http://192.168.1.3/document/
File does not exist:
/var/www/webapp/public/images/uploads/dilbert.jpg, referer:
http://192.168.1.3/document/