cpowel2
cpowel2

Reputation: 605

php move_uploaded_file trying to change file name during move

I am getting an error when i try and rename a file I am taking in from a html form into a directory using the following code. I need to alter the file name to make it unique by concatenating the $studyId onto it. The error prints out the file correctly originalName/studyId.extension. I can move the files into the "videos" folder if I do no concatentation and just use $_FILES["sonogram"]["name"] as the second argument for move_uploaded_file there are examples on php.net that use this syntax where you choose what your going to name the uploaded file so I am assuming its something silly. I should mention I am on a windows system. Any help would be greatly appreciated

$pathParts = pathinfo($_FILES["sonogram"]["name"]);
$file = $pathParts['filename'] . '/'. strval($studyId) . '.'. $pathParts['extension'];
move_uploaded_file($_FILES["sonogram"]["tmp_name"],"videos/". $file);

Upvotes: 1

Views: 1733

Answers (2)

konsolenfreddy
konsolenfreddy

Reputation: 9671

You are trying to move the file in a folder which probably doesn't exist. Your code would try to move the file to this location (with filename.txt and 1234 as $sudId):

videos/filename/1234.txt

whereas the directory filename probably doesn't exist in videos.

You could however create it first:

$pathParts = pathinfo($_FILES["sonogram"]["name"]);
$file = $pathParts['filename'] . '/'. strval($studyId) . '.'. $pathParts['extension'];
mkdir('videos/'.$pathParts['filename'],0777,TRUE);
move_uploaded_file($_FILES["sonogram"]["tmp_name"],"videos/". $file);

Upvotes: 0

jValdron
jValdron

Reputation: 3418

By concatening '/', you are telling Windows that it's in another directory. So, the file is trying to be moved to, example, "videos/filename/studyId.mp4". Which means the directory "filename" should exist. If you want to use another directory, use mkdir, or, changed the slash to an underscore or some other character.

Upvotes: 2

Related Questions