FlashNinja Posted April 12, 2012 Share Posted April 12, 2012 I'm trying out a script that lets users upload files into a directory, the file path then should be saved on the user information in the database. This script keeps throwing "Undefined index: file" errors, even though I;m sure it should be defined. Could someone take a look please? Here's the form I'm using: <form id ='change0' action ='pic_up.php' method ='post' accept-charset='UTF-8'> <fieldset > <legend>Confirm Details</legend> <input type ='hidden' name ='file' id ='file' value ='800000'/> <label for ='file' >Upload Profile Picture:</label> <input type ='file' name ='file' id ='file' /> <input type ='submit' name ='Submit' value ='Submit' /> </fieldset> </form> Here's the PHP script: <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $targ = "localhost/img/"; $targ = $targ . basename($_FILES['file']['name']); $file = ($_FILES['file']['name']); mysql_query("INSERT INTO $tablename (pic) VALUES ($file) WHERE usr = '$username'"); if(move_uploaded_file($_FILES['file']['tmp_name'], $targ)) { echo "File ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, not happening"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260821-uploading-user-files/ Share on other sites More sharing options...
MMDE Posted April 12, 2012 Share Posted April 12, 2012 Put this parameter in the form tag: enctype="multipart/form-data" example: <form action="" method="post" enctype="multipart/form-data"> Quote Link to comment https://forums.phpfreaks.com/topic/260821-uploading-user-files/#findComment-1336785 Share on other sites More sharing options...
FlashNinja Posted April 12, 2012 Author Share Posted April 12, 2012 Thanks, that fixed it so it actually runs, but now there's an error with line 22. "Warning: move_uploaded_file(c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg) [function.move-uploaded-file]: failed to open stream: Invalid argument in C:\xampp\htdocs\pic_up.php on line 22 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\php45A3.tmp' to 'c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg' in C:\xampp\htdocs\pic_up.php on line 22" I'm not quite sure what on line 22 is actually causing this error. PHP script: <?php include 'connect.php'; session_start(); $_SESSION['username']; $username = $_SESSION['username']; if(!(isset($_SESSION['login']) && $_SESSION['login']!= " ")){ header("Location: login.php"); } $tablename = 'usr_test'; $targ = "c:\xampp\htdocs\img\\"; $targ = $targ . basename($_FILES['file']['name']); $file = ($_FILES['file']['name']); mysql_query("INSERT INTO $tablename (pic) VALUES ('$file') WHERE usr = '$username'"); if(move_uploaded_file($_FILES['file']['tmp_name'], $targ)) { echo "File ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; } else { echo "Sorry, not happening"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/260821-uploading-user-files/#findComment-1336794 Share on other sites More sharing options...
xyph Posted April 12, 2012 Share Posted April 12, 2012 c: mpp\htdocs\img\community-nbc-dan-harmon-abed-evolution-320.jpg seems wrong doesn't it? Apache will support using forward slashes in your local paths: c:/webserver/www/ Alternately, you can just use $_SERVER['DOCUMENT_ROOT']. This should return C:/xampp/htdocs/ Quote Link to comment https://forums.phpfreaks.com/topic/260821-uploading-user-files/#findComment-1336815 Share on other sites More sharing options...
FlashNinja Posted April 12, 2012 Author Share Posted April 12, 2012 I didn't even noticed I did that with the directory path, oops. Well it works now - other than the file path isn't being written to the database - but I can probably fix that myself. Thanks guys. Quote Link to comment https://forums.phpfreaks.com/topic/260821-uploading-user-files/#findComment-1336818 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.