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!";
}