假设我正在为一个项目设置上传功能。 我正在使用 PHP5,我想知道通过使用 $_FILES 超全局数组,我是否可以访问“tmp_name”数组键,它是正在上传的文件的临时路径和名称,以显示图像的预览,然后将其移动到服务器中的“上传”文件夹。
我的想法是这样的: 使用 jQuery 检测"file"输入字段何时发生更改(当用户选择图像文件时),然后发出 ajax 调用来上传文件,这甚至在我使用“move_uploaded_file”之前会将其移动到临时文件夹。 如果我可以以某种方式访问“tmp_name”数组键,我可能可以将其放入“img”标签中以显示预览。 稍后单击提交按钮时将文件移动到最终位置。
我不是要求任何代码示例或任何东西,因为我很高兴自己完成这个任务,我只是想知道是否可以以任何方式访问该数组键。
最佳答案
是的,可以。
In PHP, when a user submits a form with a the $_FILES superglobal is populated with useful information about the uploaded file. Inside you’ll find each uploaded file’s original name, content-type, temporary upload location on the server, an error code, and the size in bytes.
取自 Fixing the $_FILES superglobal
这是从 $_FILES
数组网站获取的示例 print_r
输出,
Array
(
[download_zip] => Array
(
[name] => dummy.txt
[type] => text/plain
[tmp_name] => /Applications/MAMP/tmp/php/php5TBPsw
[error] => 0
[size] => 1
)
[download_screenshot] => Array
(
[name] => dummy.txt
[type] => text/plain
[tmp_name] => /Applications/MAMP/tmp/php/phpTncd39
[error] => 0
[size] => 1
)
)
您可以使用 $files_array['download_zip']['tmp_name']
语法访问 tmp_name
。
更新:
$path = $files_array['download_zip']['tmp_name'];
echo "<img src='$path'>";
关于PHP 文件上传,使用 FILES 超全局数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2785629/