2

I'm creating a simple file upload script in PHP to upload XML files (it's actually a WordPress Plugin), I suspect I'm having issues passing the file from the temp folder to my designated folder however. The complete script is shown below, the script actually runs and succeeds, the echo statement "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded."; displays. However the XML file never appears in the destination directory. Interestingly if I attempt to upload the same file twice the if statement returns which dictates the file already exists is returning true. Apologies if this is an obvious issue, I'm very new to PHP.

$target_dir = "/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files";
 (upload) from the form
$target_file = $target_dir . basename($_FILES["upload"]["name"]);
$success = 1;
$type = pathinfo($target_file,PATHINFO_EXTENSION);

if((!empty($_FILES["upload"]))) 
{
    if (file_exists($target_file)) {
        echo "Sorry, file already exists, please archive or remove existing xml files before uploading a new file.";
        $success = 0;
    }
        if ($_FILES["upload"]["size"] > 500000) {
            echo "Sorry, your file is too large.";
            $success = 0;
        }
            if($type != "xml") 
            {
                echo "Only XML file types are allowed";
                $success = 0;
            }
                if ($success == 0) 
                {
                    echo "Error uploading.";
                } 
                    else 
                    {
                        if (move_uploaded_file($_FILES["upload"]["tmp_name"], $target_file)) 
                        {
                            echo "The file ". basename( $_FILES["upload"]["name"]). " has been uploaded.";
                        } 
                            else 
                            {
                                echo "Sorry, there was an error uploading your file.";
                            }       
                     }
    }
    else
    {
    echo "Please upload a file!";
    }
Liam Fell
  • 1,308
  • 3
  • 21
  • 39

1 Answers1

0

Issue is in $target_dir path. Root directory path is wrong. Use $_SERVER['DOCUMENT_ROOT'].

Change this line

$target_dir = "/user1/caravans/public_html/wordpress/wp-content/uploads/wpallimport/files";

To

$target_dir = $_SERVER['DOCUMENT_ROOT']."/wordpress/wp-content/uploads/wpallimport/files";

This way all files will be uploaded to "files" directory.

Now file being uploaded in wrong directory. So that why you can not see them in files directory.

In Linux Server(Ubuntu, CentOS) user directory is not root directory. It exist under "home" directory. But it may differ according to LINUX flavor and version.

echo $_SERVER['DOCUMENT_ROOT'];

This will help you to find your Root directoy.

Qaisar Shabbir
  • 103
  • 2
  • 10