Question

I am running a PHP script to upload files from a HTML form, rename them and place them on my server. It is uploading and renaming, however every file name now starts with the word "Array"
ie: 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.

//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. I've tried googling but as soon as I mention "Array" my google results are nothing remotely the same.

Ok so thanks to below I solved it. For anyone in the future I removed the entire $_FILES array so the code reads as

//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.

Was it helpful?

Solution

Should be

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

You forgot to include ["name"] after $_FILES["uploaded"]

OTHER TIPS

In your move_uploaded_file you are using the whole $_FILE['uploaded'] array as the name, not just the filename. A small tweak:

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

And you should be all good.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top