PHP Rename file if exists -



PHP Rename file if exists -

i've been working image uploading , wondering why isn't working correctly? doesn't move/upload file new name if exists.

if(isset($_request['submit'])){ $filename= $_files["imgfile"]["name"]; if ((($_files["imgfile"]["type"] == "image/gif")|| ($_files["imgfile"]["type"] == "image/jpeg") || ($_files["imgfile"]["type"] == "image/png") || ($_files["imgfile"]["type"] == "image/pjpeg")) && ($_files["imgfile"]["size"] < 20000000)){ $loc = "userpics/$filename"; if(file_exists($loc)){ $increment = 0; list($name, $ext) = explode('.', $loc); while(file_exists($loc)) { $increment++; $loc = $name. $increment . '.' . $ext; $filename = $name. $increment . '.' . $ext; } move_uploaded_file($_files["imgfile"]["tmp_name"],"userpics/$loc"); } else{ move_uploaded_file($_files["imgfile"]["tmp_name"],"userpics/$filename"); } } else{ echo "invalid file."; } }

you've included folder path in $loc, effort move file userpics/$loc, incorrect. see comments:

$filename = "example.jpg"; $loc = "userpics/$filename"; if(file_exists($loc)){ $increment = 0; list($name, $ext) = explode('.', $loc); while(file_exists($loc)) { $increment++; // $loc "userpics/example1.jpg" $loc = $name. $increment . '.' . $ext; $filename = $name. $increment . '.' . $ext; } // you're trying move uploaded file "userpics/$loc" // expands "userpics/userpics/example1.jpg" move_uploaded_file($_files["imgfile"]["tmp_name"],"userpics/$loc"); } else { // ...

as general debugging tip, always check function's homecoming value see if successful. secondly, display function's input values if it's failing. create debugging things lot easier.

php file if-statement rename exists

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -