0

I'm having trouble with setting up the file upload to my php script.

Within the upload itself, I have it directed towards the path I want, but I'm not sure if I have the rest of the script syntactically right.

EDIT: I have resolved my situation with the file not properly uploading. I have edited the blocks of code to what I have currently working. If needed, please refer to the pre-edited version to see changes :).

Cheers.

$tardir= "C:/xampp/htdocs/uploaddir/$targetname";
$uploadOk=1;

if(isset($_FILES["uploadFile"])){
    $tardir= $tardir . "/" . basename($_FILES["uploadFile"]["name"]);
    if(move_uploaded_file($_FILES["uploadFile"]["tmp_name"], $tardir)){ 
        echo "The file: ". basename($_FILES["uploadFile"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}

The $targetnameis set to whatever the user inputs in the field. In this case, I have it as their email.

$targetname = filter_input(INPUT_POST, 'email');

This is what the <html> for the file upload looks like:

<form action= "userlogin.php" method= "post" enctype= "multipart/form-data">
        <p><strong>Please choose a file to upload: <input type="file" name="uploadFile"><br></p>
           <input type="submit" value="Upload File">
8
  • In the error, which variable is throwing an undefined index error? Commented Oct 16, 2014 at 23:20
  • uploadFile is throwing the error twice. @ColinSchoen Commented Oct 16, 2014 at 23:22
  • Careful... you're letting people upload whatever they want to your server, and you're keeping it in the doc root. Someone can upload malicious scripts and execute them easily. Commented Oct 16, 2014 at 23:22
  • It's just for a lab exercise in our class. It isn't a live server. Just for my practical use and testing :) @Brad Commented Oct 16, 2014 at 23:23
  • Is file_uploads = On in your php.ini? Commented Oct 16, 2014 at 23:28

1 Answer 1

1

It's hard to guess all your code, as it seems you only have a snippet here, but I'm gonna take a wild guess. If target name comes from a text input like you say and you have it set to e-mail, are you leaving off a slash here?

$tardir= "C:/xampp/htdocs/uploaddir/$targetname";

This would be like:

C:/xampp/htdocs/uploaddir/[email protected]

This runs:

$tardir= $tardir . basename($_FILES["uploadFile"]["name"]);

then:

 C:/xampp/htdocs/uploaddir/[email protected]

shouldn't it be:

C:/xampp/htdocs/uploaddir/[email protected]/filename

you can also comment out all your code here and var_dump() your file array, and echo out your paths before you try using them to see what data is held in each of your containers. If I get a bug like this I dump everything and echo every string until I find it..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.