ARRAY在上传后重命名为文件名 - PHP

时间:2012-07-08 06:11:09

标签: php arrays renaming

我正在运行一个PHP脚本来从HTML表单上传文件,重命名它们并将它们放在我的服务器上。它正在上传和重命名,但现在每个文件名都以单词“Array”开头 即:ArrayTest Document v1.0.pdf
我相信它不是我的$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.";
        }
}

我觉得它与$FILES['uploaded']命令中的move_uploaded_file部分有关。我已经尝试使用谷歌搜索,但只要我提到“数组”,我的谷歌搜索结果就没那么远了。

好的,谢谢以下我解决了。对于将来的任何人,我删除了整个$_FILES数组,因此代码为

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

现在所有内容都使用正确的名称结构上传。感谢。

2 个答案:

答案 0 :(得分:0)

应该是

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

您忘记在$ _FILES [“上传”]

之后加入[“name”]

答案 1 :(得分:0)

在你的move_uploaded_file中,你使用整个$_FILE['uploaded']数组作为名称,而不仅仅是文件名。一个小小的调整:

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

你应该都很好。