美好的一天。我正在将图像文件上传到我的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
但是在检查时,实际上传了一张图片,但文件名已关闭。
答案 0 :(得分:0)
我做到了。我认为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
而不是$fullPath
,我不知道为什么。是因为它已被移动,现在有一个不同的名字?