0

I'm having trouble moving the file. I'm also wondering if there's a way for a user to upload a different picture, and have it automatically remove the old one?

move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/".$_POST['username']."/profilepic".$_FILES["profilepic"]["name"]);
9
  • Dont suppress the errors using @ and see what you get. Commented Aug 31, 2012 at 5:27
  • Are you sure the relative path to your destination exists and is writable? Also, to overwrite old ones, simply use a static filename (you are already in a distinct user's directory...) Commented Aug 31, 2012 at 5:31
  • Make sure you have write permission on the folder you're uploading the picture Commented Aug 31, 2012 at 5:32
  • @Deepak its still not working, its not moving the file to the correct place Commented Aug 31, 2012 at 5:33
  • its moving to userdata\plmexico\profilepic ,, but the plmexico is a user Commented Aug 31, 2012 at 5:34

2 Answers 2

1

You have remove @ before $_FILES and check error. if you want to replace picture old to new then make your own user wise image name and use it like.

$_POST['username']=test;

$imagname=$_POST['username']."_img";

 move_uploaded_file(@$_FILES["profilepic"]["tmp_name"],"userdata/".$_POST['username']."/profilepic/".$imagename);
Sign up to request clarification or add additional context in comments.

Comments

0

Try this block of code

    $imagename = $_FILES['profilepic']['name'];
    $source = $_FILES['profilepic']['tmp_name'];
    $imagename = str_replace(" ", "_", $imagename);
    $target = "userdata/".$_POST['username']."/profilepic" . $imagename;
    move_uploaded_file($source, $target);

Make sure you have got write permission to the folder userdata,plmexico,profilepic.

Good habits:

Rename the user's file with a name generated by your algorithm. As you want to replace the profile pic when they upload a new picture you naming can combine user_id with the file name. So the code becomes.

    $localname = $_FILES['profilepic']['name'];
    $imagename = $user_id."_profile".explode(end(",",$localname)); // $user_id is the user's id
    $source = $_FILES['profilepic']['tmp_name'];
    $imagename = str_replace(" ", "_", $imagename);
    $target = "userdata/".$_POST['username']."/profilepic" . $imagename;
    move_uploaded_file($source, $target);

This code doesn't work if the user uploads an image in different format.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.