Hello everybody

I made an personal form to upload files to different maps wich can be selected.
I'm making an mistake, but I don't know how to fix this. There is no error only when the file is processed, the file isn't kept in the directory map wich was selected.
It echoes: "There was an error uploading the file, please try again!Smile...

This is my form:

<form enctype="multipart/form-data" action="upload.php" method="post"> 
            <input type="hidden" name="MAX_FILE_SIZE" value="100000" /> 
            Choose a file to upload: 
  <input name="uploadedfile" type="file" /> 
  <select name="uploads" size="1"> 
    <option>uploads</option> 
    <option>files</option> 
  uploads</select> 
  <br /> 
  <input type="submit" value="Upload File" /> 
          </form> 

And here is my upload script.

<?php 
$sTargetPath = 'c:/wamp/www/uploading/uploads/'.((isset($_POST['uploads']) && is_string($_POST['uploads']))? $_POST['uploads'].'/' : ''); 
if(is_dir($sTargetPath) && move_uploaded_file($_FILES['uploadedfile']['tmp_name'] 
     . basename($_FILES['uploadedfile']['tmp_name']), $sTargetPath)) { 

    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded"; 

} 
 else{ 
    echo "There was an error uploading the file, please try again!"; 
} 
echo "smile..."; 
?>  

Thanks for every help and feedback

Dani AI

Generated

The upload fails because of two concrete issues in 's script: the call to move_uploaded_file is malformed (the arguments were accidentally concatenated) and the destination passed to move_uploaded_file must be a complete filepath (directory + filename), not a directory alone. Also double-check the HTML <select> (there’s stray text inside it) and avoid trusting the posted folder name without sanitizing it.

A minimal, safer server-side pattern that fixes the problems:

<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);

$baseDir = 'c:/wamp/www/uploading';                 // or use a relative path
$folder  = isset($_POST['uploads']) ? basename($_POST['uploads']) : 'uploads';
$targetDir = rtrim($baseDir, '/\\') . DIRECTORY_SEPARATOR . $folder;

if (!is_dir($targetDir) || !is_writable($targetDir)) {
    exit("Target directory unavailable: $targetDir");
}

if (empty($_FILES['uploadedfile']) || $_FILES['uploadedfile']['error'] !== UPLOAD_ERR_OK) {
    exit("Upload error code: " . ($_FILES['uploadedfile']['error'] ?? 'no file'));
}

$filename = basename($_FILES['uploadedfile']['name']);
$destination = $targetDir . DIRECTORY_SEPARATOR . $filename;

if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $destination)) {
    echo "File uploaded to: $destination";
} else {
    echo "Failed to move uploaded file.";
}
?>

Quick debugging checklist

  • Dump $_FILES (var_dump) to confirm tmp_name exists.
  • Check upload_max_filesize and post_max_size in php.ini and restart Apache.
  • Ensure the target directory exists and is writable by the web server.
  • Use basename() on both the selected folder and the uploaded filename to avoid path traversal.

Notes on paths and security

  • As suggested, relative paths simplify deployment; PHP accepts forward slashes on Windows so you generally do not need to escape backslashes.
  • Sanitize/validate the selected folder and filename, restrict extensions, and consider generating unique filenames (uniqid or a timestamp) to avoid overwrites.

$sTargetPath = 'c:/wamp/www/uploading/uploads/'

it is better and more convenient to use relative path instead of absolute path.

$sTargetPath = 'uploading/uploads/'

Windows use \ as separator, if you insist to use absolute path. Don't forget to add another one as escape character.

'c:\\wamp\\www\\uploading\\uploads\\'
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.