[英]PHP - Getting the filepath of the file after uploading to server
Good day. 美好的一天。 I am uploading an image file to my php server. 我正在将图像文件上传到我的PHP服务器。 I am successfully uploading the file to a folder. 我已成功将文件上传到文件夹。 What I want to do now is as I assemble my JSON response, I want to get the new file path of the file so I would know where it is programatically. 我现在想做的是,在组装JSON响应时,我想获取文件的新文件路径,这样我就可以知道它在程序上的位置。
Here is my php code so far: 到目前为止,这是我的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
)));
?>
I am trying to get the new file path which is /gallery/
+ the filename, however, I am getting an invalid JSON Object when I process it in my ExtJS part. 我正在尝试获取新的文件路径,即/gallery/
+文件名,但是,在ExtJS部分中处理它时,我得到的是无效的JSON对象。
Can anyone help me with my problem? 谁能帮我解决我的问题? I also tried sprintf('./gallery/%s.%s', sha1_file($_FILES['fileToUpload']['tmp_name']);
but that doesn't seem to work. 我也尝试了sprintf('./gallery/%s.%s', sha1_file($_FILES['fileToUpload']['tmp_name']);
但这似乎不起作用。
Any help is very much appreciated. 很感谢任何形式的帮助。
Edit: 编辑:
So far this works: 到目前为止,这有效:
$uploaddir = '/gallery/';
$uploadFile = $uploaddir . basename($_FILES['fileToUpload']['tmp_name']);
die(json_encode(array(
'success' => true,
'status' => "File is uploaded successfully. - 8",
'filePath' => $uploadFile
)));
However, I don't have the file extension and I get something like: /gallery/phptnLZJm
but upon checking, an image is actually uploaded but the filename is off. 但是,我没有文件扩展名,但是却得到类似: /gallery/phptnLZJm
但是经过检查,实际上已上传图像,但文件名已关闭。
I made it work. 我成功了 I think it sha1_file
was the issue. 我认为这是sha1_file
的问题。 The code is now: 现在的代码是:
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"
)));
Before, I would call sha1_file
instead of basename
as I am assembling my $fullPath
, and I don't know why. 以前,在组装$fullPath
时,我将称呼sha1_file
而不是basename
,我也不知道为什么。 Was it because it was moved already and now has a different name? 是因为它已经被移动并且现在有了另一个名称了吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.