Unable to move files to folder in php

while trying to allow users to upload images of theirs into my site, I did this(Using the pattern taught in php: noviceToNinja 6th edition coauthored by @kevinyank and @TomB).

<li>
  <label for="pic">Image</label>
  <input type="hidden" name="MAX_FILE_SIZE" value="<?=$max; ?>">
  <input type="file" name="user[pic]" id="pic">
<li>

And in the registration controller, I did this(after frustrated, just to make sure it upload successfully), and it dosent work…

$file = $_FILES['user']['pic']['name'] . time() . $_SERVER['REMOTE_ADDR'] . '.jpeg';
		//$user['pic'] = $file;
		//$destination = 'C:/me/ijdb/Project/public/imgs/';
		$filename = 'C:\me\ijdb\Project\public\imgs\\' . $file;
		if (!is_uploaded_file($_FILES['user']['pic']['tmp_name']) or !copy($_FILES['user']['pic']['tmp_name'], $filename)){
			$valid = false;
			$errors[] = "Could not save file as $filename!";
		}

NB: just extract the image section as it dosen’t work and I thought I have to use $_FILES['user']['pic']['tmp_name'] to extract or get the neccessary info just as plain text like $user['username'](will get what is in the username textarea).

Please, who can put me through how to do it?

You should use move_uploaded_file instead of copy. Otherwise, check is_uploaded_file is returning true, if not, use var_dump($_POST) to check that the file is being uploaded correctly.

$filename = 'C:\me\ijdb\Project\public\imgs\\' . $file;

What’s the reason for the double backslash at the end of the path?

I think you mean var_dump($_FILES);? :slightly_smiling_face:

1 Like

Yes, you’re right!

thanks all. It now working but the only problem is it doesn’t move the file to the imgs folder which is located inside the public folder but put it in the public folder. How can I go about it

Did you change the code at all? Could your post the code you are using now?

No. Just this

$destination = 'c:/me/ijdb/Project/public/imgs';
And this is the uploading code

$user['pic'] = sha1( $_FILES['image']['name'] . time() . $_SERVER['REMOTE_ADDR'] . uniqid('',true)). $ext;

			move_uploaded_file($_FILES['image']['tmp_name'], $destination);

As $destination is the destination filename, not just the folder, it seems that you are asking it to move the image into a file called “c:\me\ijdb\project\public\imgs”, not into a folder of that name.

http://php.net/manual/en/function.move-uploaded-file.php

yes. That is what I want. i want it to be move to that folder

Just ask you the question: in that folder under which filename? Because that’s what the function needs to know. You have to give a full path.

1 Like

@tomB i got the error finally, it has to do with the .htaccess. it says warning: cannot write to folder .i.e after doing it the right way

@tomB this is the message

Warning: move_uploaded_file(c:/me/ijdb/Project/public/imgs/tmp/phpiUJIbr): failed to open stream: No such file or directory in /home/vagrant/Code/Project/classes/Ndb/Controllers/Register.php on line 128

and the uploaded file script again

move_uploaded_file($_FILES['image']['tmp_name'], $destination . $_FILES['image']['tmp_name']);

Print out source and destination and you’ll see what the problem witht that code is.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.