我正在将图像文件上传到我的 PHP 服务器。我已成功将文件上传到文件夹。我现在想做的是,当我组装 JSON 响应时,我想获取该文件的新文件路径,这样我就能以编程方式知道它在哪里。
这是到目前为止我的 php 代码:
<?php
header('Content-Type: text/html; charset=utf-8');
if (
!isset($_FILES['fileToUpload']['error']) ||
is_array($_FILES['fileToUpload']['error'])
) {
die(json_encode(array(
'success' => false,
'status' => "Invalid Parameters. - 1",
'files' => $_FILES
)));
}
// Check $_FILES['fileToUpload']['error'] value.
switch ($_FILES['fileToUpload']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
die(json_encode(array(
'success' => false,
'status' => "No file sent. - 2",
'files' => $_FILES
)));
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
die(json_encode(array(
'success' => false,
'status' => "Exceeded filesize limit. - 3",
'files' => $_FILES
)));
default:
die(json_encode(array(
'success' => false,
'status' => "Unknown errors. - 4",
'files' => $_FILES
)));
}
// You should also check filesize here.
if ($_FILES['fileToUpload']['size'] > 1000000) {
die(json_encode(array(
'success' => false,
'status' => "Exceeded File Size Limit. - 5",
'files' => $_FILES
)));
}
// DO NOT TRUST $_FILES['fileToUpload']['mime'] VALUE !!
// Check MIME Type by yourself.
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['fileToUpload']['tmp_name']),
array(
'jpg' => 'image/jpeg',
'png' => 'image/png',
'gif' => 'image/gif',
),
true
)) {
die(json_encode(array(
'success' => false,
'status' => "Invalid file format. - 6",
'files' => $_FILES
)));
}
// You should name it uniquely.
// DO NOT USE $_FILES['fileToUpload']['name'] WITHOUT ANY VALIDATION !!
// On this example, obtain safe unique name from its binary data.
if (!move_uploaded_file(
$_FILES['fileToUpload']['tmp_name'],
sprintf('./gallery/%s.%s',
sha1_file($_FILES['fileToUpload']['tmp_name']),
$ext
)
)) {
die(json_encode(array(
'success' => false,
'status' => "Failed to move uploaded file. - 7",
'files' => $_FILES
)));
}
$filePathFull = sprintf('./gallery/%s.%s',
sha1_file($_FILES['fileToUpload']['tmp_name']);
$uploaddir = '/gallery/';
$uploadfile = $uploaddir . basename($_FILES['fileToUpload']['tmp_name']);
die(json_encode(array(
'success' => true,
'status' => "File is uploaded successfully. - 8",
'files' => $_FILES,
'filePath' => $uploadfile
)));
?>
我正在尝试获取新文件路径,即
/gallery/
+ 文件名,但是,当我在 ExtJS 部分中处理它时,我得到了一个无效的 JSON 对象。
接下来我可以尝试什么?我也尝试过
sprintf('./gallery/%s.%s', sha1_file($_FILES['fileToUpload']['tmp_name']);
,但似乎不起作用。
到目前为止,这有效:
$uploaddir = '/gallery/';
$uploadFile = $uploaddir . basename($_FILES['fileToUpload']['tmp_name']);
die(json_encode(array(
'success' => true,
'status' => "File is uploaded successfully. - 8",
'filePath' => $uploadFile
)));
但是,我没有文件扩展名,并且收到类似以下内容:
/gallery/phptnLZJm
,但经过检查,图像实际上已上传,但文件名已关闭。
我成功了。我认为这就是问题所在。现在的代码是:
sha1_file
以前,当我组装我的
if (!move_uploaded_file(
$_FILES['fileToUpload']['tmp_name'],
sprintf('./gallery/%s.%s',
basename($_FILES['fileToUpload']['tmp_name']),
$ext
)
)) {
die(json_encode(array(
'success' => false,
'status' => "Failed to move uploaded file. - 7",
'files' => $_FILES
)));
}
// $uploadFile = sprintf('./gallery/%s.%s',
// sha1_file($_FILES['fileToUpload']['tmp_name']);
$uploaddir = '/gallery/';
// $uploadFile = $uploaddir . basename($_FILES['fileToUpload']['tmp_name']);
$name = $_FILES["fileToUpload"]["tmp_name"];
$fullPath = sprintf('/gallery/%s.%s',
basename($_FILES['fileToUpload']['tmp_name']),
$ext
);
die(json_encode(array(
'success' => true,
'status' => "File is uploaded successfully. - 8",
'filePath' => "$fullPath"
)));
时,我会调用
sha1_file
而不是basename
,我不知道为什么。是因为它已经被移动并且现在有不同的名称吗?