user5542300
user5542300

Reputation:

Unable to upload file via php

I'm trying to upload a file and then save it by changing it's name. However, it doesn't work and give me this error.

Warning: move_uploaded_file(uploads/564b68ef0e2f8|3d-pc-nature-wallpaper.jpg): failed to open stream: Invalid argument in C:\wamp\www\f\ajax.php on line 157

Warning: move_uploaded_file(): Unable to move 'C:\wamp\tmp\phpA364.tmp' to 'uploads/564b68ef0e2f8|3d-pc-nature-wallpaper.jpg' in C:\wamp\www\f\ajax.php on line 157

I have verified and the file is correctly going to $_FILES. Here's my code.

$rand_img = uniqid();
$file_upload_folder = "uploads";
$finalImgLink = $file_upload_folder . '/' . $rand_img . '|' . $_FILES['file']['name'];
//move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/$rand_img|' . $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $finalImgLink))
{
    echo "ok";
}
else
{
    echo "not ok";
}

What is wrong here?

Upvotes: 5

Views: 142

Answers (2)

Akshay
Akshay

Reputation: 2229

You can't use | in your filename. If you want a separator, you can have something like , . or any specific string like abc, xy etc,

So update your code in the following way.

$finalImgLink = $file_upload_folder . '/' . $rand_img . ".." . $_FILES['file']['name'];

Upvotes: 1

Tristan
Tristan

Reputation: 3321

You cannot use a pipe (|) in file names. This is the most likely cause of the error you are experiencing. Try change it to an underscore or dash.

$rand_img = uniqid();
$file_upload_folder = "uploads";
$finalImgLink = $file_upload_folder . '/' . $rand_img . '_' . $_FILES['file']['name'];
//move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/$rand_img|' . $_FILES['file']['name']);
if(move_uploaded_file($_FILES['file']['tmp_name'], $finalImgLink))
{
    echo "ok";
}
else
{
    echo "not ok";
}

On windows, filename cannot contain any of the following characters:

\ / : * ? " < > |

Upvotes: 4

Related Questions