【发布时间】:2018-01-30 03:52:12
【问题描述】:
首先,上传文件夹是 777,我的旧上传脚本可以正常工作,因此服务器接受文件。这是一个新的目的地。
我使用 krajee 引导上传来发送文件。我收到了 Jason 的回复。该错误似乎与移动上传的文件有关。我敢打赌这是我这边的一个简单错误,但我看不到它。
<?php
if (empty($_FILES['filer42'])) {
echo json_encode(['error'=>'No files found for upload.']);
// or you can throw an exception
return; // terminate
}
// get the files posted
$images = $_FILES['filer42'];
// a flag to see if everything is ok
$success = null;
// file paths to store
$paths= [];
// get file names
$filenames = $images['name'];
// loop and process files
for($i=0; $i < count($filenames); $i++){
$ext = explode('.', basename($filenames[$i]));
$target = "uploads" . DIRECTORY_SEPARATOR . md5(uniqid()) . "." . array_pop($ext);
if(move_uploaded_file($_FILES["filer42"]["tmp_name"][$i], $target)) {
$success = true;
$paths[] = $target;
} else {
$success = false;
break;
}
}
// check and process based on successful status
if ($success === true) {.
$output = [];
$output = ['uploaded' => $paths];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];
// delete any uploaded files
foreach ($paths as $file) {
unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}
// return a json encoded response for plugin to process successfully
echo json_encode($output);
?>
【问题讨论】:
-
错误本身是什么?
-
你为什么使用两个文件名? 1.
$_FILES['filer42']。 & 2.$_FILES['pictures']. -
$target返回string(42) "uploads/a0029f80fce0bd52a982b0159af6e33d.0"但这必须是.jpg而不是.0来看看为什么会发生这种情况。