简体   繁体   English

上传后,ARRAY在重命名文件名之前-PHP

[英]ARRAY is preceding renamed filenames after upload - PHP

I am running a PHP script to upload files from a HTML form, rename them and place them on my server. 我正在运行一个PHP脚本,以从HTML表单上载文件,对其进行重命名并将其放置在我的服务器上。 It is uploading and renaming, however every file name now starts with the word "Array" 它正在上传和重命名,但是现在每个文件名都以单词“ Array”开头
ie: ArrayTest Document v1.0.pdf 即: ArrayTest Document v1.0.pdf
I am fairly confident it isn't my $newfn variable as I display that in a table and it doesnt show up. 我相当有信心,这不是我的$newfn变量,因为我在表中显示该变量,并且该变量不会显示。

//This gets all the other information from the form
$name=$_POST['docname'];
$version=$_POST['docver'];
$date=$_POST['docdate'];
$type=$_POST['doctype'];
$author=$_POST['docauth'];
$file=$_FILES['uploaded']['name'];

//target directory is assigned
$target = $directory;
$target = $target . basename($file) ;

//grab file extension
$filetypes = array(
'image/png' => '.png',
'image/gif' => '.gif',
'image/jpeg' => '.jpg',
'image/bmp' => '.bmp',
'application/pdf' => '.pdf');
$ext = $filetypes[$_FILES['uploaded']['type']];

//generate new filename variable "name v??.xxx"
$newfn = $name . ' ' . 'v' . $version . $ext;

//Check to see if file exists
if (file_exists($directory . $file))
{
echo $_FILES["uploaded"]["name"] . " already exists. ";
}
else
{
//if its a new file, change name and upload
    if (move_uploaded_file($_FILES["uploaded"]["tmp_name"],
        $directory . $_FILES["uploaded"] . $newfn))
        {
        echo "The file ". basename($file). " has been uploaded";
        }
        else
        {
        echo "Sorry, there was a problem uploading your file.";
        }
}

I feel like it's related to my $FILES['uploaded'] section in the move_uploaded_file command. 我觉得它与move_uploaded_file命令中的$FILES['uploaded']部分有关。 I've tried googling but as soon as I mention "Array" my google results are nothing remotely the same. 我已经尝试使用Google搜索,但是一旦提到“数组”,我的Google搜索结果就几乎没有相同之处。

Ok so thanks to below I solved it. 好了,感谢下面我解决了它。 For anyone in the future I removed the entire $_FILES array so the code reads as 对于以后的任何人,我都删除了整个$_FILES数组,因此代码显示为

//if its a new file, change name and upload
if (move_uploaded_file($_FILES["uploaded"]["tmp_name"],
    $directory . $newfn))

Everything now uploads with the correct name structure. 现在,所有内容都将以正确的名称结构上传。 Thanks. 谢谢。

Should be 应该

move_uploaded_file($_FILES["uploaded"]["tmp_name"],$directory . $_FILES["uploaded"]["name"] . $newfn)

You forgot to include ["name"] after $_FILES["uploaded"] 您忘记在$ _FILES [“ uploaded”]之后加入[“ name”]

In your move_uploaded_file you are using the whole $_FILE['uploaded'] array as the name, not just the filename. 在move_uploaded_file文件中,您将使用整个$_FILE['uploaded']数组作为名称,而不仅仅是文件名。 A small tweak: 一个小调整:

if (move_uploaded_file($_FILES["uploaded"]["tmp_name"],
        $directory . $_FILES["uploaded"]["name"] . $newfn))
        {

And you should be all good. 而且你应该一切都好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM