-1

I'm working on a backoffice to uploaded images so you can change the images in the front. Problem is my upload script isn't returning me any errors to work with, but it's also not moving the file. I want my script to upload the new file to that location and replace any file with the same name regardless, but I'd have to get the moving working first.

Where am I going wrong?

================================================================================== EDIT: I've added the slash to the path. It still won't upload my image though. Code is as follows:

$target_path = "../../site/images/user_images/";
$image_name = $_POST['filename'].".".pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION);

if(move_uploaded_file($_FILES['file']['tmp_name'], $target_path.$image_name)) {
    echo "<meta http-equiv='refresh' content='0;URL=images.php?saved=true' />";
} else {
    echo "Error";
}
Parrotmaster
  • 647
  • 1
  • 7
  • 27

5 Answers5

3
$target_path = "../../site/images/user_images/";
                                             ^ 

Here you are forgetting / which tells that it is directory where you have to upload the file.

So $target_path.$image_name will become like this

../../site/images/user_images/newfile.png

Else your code produce this output

../../site/images/user_imagesnewfile.png
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
1

You're missing the trailing slash. You need:

$target_path = "../../site/images/user_images/";
  • Thanks, that fixed the issue of not uploading. Except now it uploads the file with the wrong name (due to $target_path.$image_name in my code i presume). Would you know how to upload it with the proper name to the location? When I try ($target_path, $image_name) instead it returns "Error". Also it uploaded it to the folder above user_images, so it ended up in images/ instead – Parrotmaster Jul 15 '13 at 10:50
  • @Parrotmaster: It should work. Analyze the [`error codes`](http://www.php.net/manual/en/features.file-upload.errors.php) and find out what's wrong. –  Jul 15 '13 at 11:02
  • @Parrotmaster: yes, that's the default behaviour. You'll have to request it explicity. `echo $_FILES['file']['error'];` –  Jul 15 '13 at 11:08
  • Yes I know, it's returning 0. – Parrotmaster Jul 15 '13 at 11:09
  • Apparently it is working. My FTP client didn't how the images even after a few reloads. – Parrotmaster Jul 15 '13 at 12:01
0

add slashe to end of the path variable, and try again.

$target_path = "../../site/images/user_images/";
jaydeep namera
  • 1,024
  • 7
  • 15
0

have you added enctype in your form?

<form name="form1" id="form1" method="post" action="" enctype="multipart/form-data">
</form>
chirag ode
  • 950
  • 7
  • 15
0
$target_path = $_SERVER['DOCUMENt_ROOT']."your/folder/relat";

the path should be complete path and after that assign proper permissions to the folder