dharm Posted August 25, 2006 Share Posted August 25, 2006 Hi, I am having trouble with a simple upload file script and would appreciate any help.. Could anyone tell me how I could make files upload to my localhost (for testing)..here is the script..<html><head><title>File Upload Form</title></head><body><? if (!isset($action)) {?>This form allows you to upload a file to the server.<br>[code]<form action="getfile.php" method="post"><br>Type (or select) Filename: <input type="file" name="uploadFile"><input type="hidden" name="MAX_FILE_SIZE" value="25000" /><input type="hidden" name="action" value="fghgf" /><input type="submit" value="Upload File"></form></body></html><? } ?><? if (isset($action)) {?><html><head><title>Process Uploaded File</title></head><body><?phpecho $_FILES['uploadFile'] ['name'];if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], "Home/uploads/{$_FILES['uploadFile'] ['name']}") ) { print '<p> The file has been successfully uploaded </p>'; }else { switch ($_FILES['uploadFile'] ['error']) { case 1: print '<p> The file is bigger than this PHP installation allows</p>'; break; case 2: print '<p> The file is bigger than this form allows</p>'; break; case 3: print '<p> Only part of the file was uploaded</p>'; break; case 4: print '<p> No file was uploaded</p>'; break; } }?></body></html><? } ?>[/code]here is the output..Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 27Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 28Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 29Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 34 Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 25, 2006 Share Posted August 25, 2006 The manual very clearly states:[quote]<!-- The data encoding type, enctype, MUST be specified as below --><form enctype="multipart/form-data" action="__URL__" method="POST"> <!-- MAX_FILE_SIZE must precede the file input field --> <input type="hidden" name="MAX_FILE_SIZE" value="30000" /> <!-- Name of input element determines name in $_FILES array --> Send this file: <input name="userfile" type="file" /> <input type="submit" value="Send File" /></form>[/quote]http://us3.php.net/manual/en/features.file-upload.php Quote Link to comment Share on other sites More sharing options...
dharm Posted August 25, 2006 Author Share Posted August 25, 2006 Thx!..Got it working.. but if I place each part of the array into separate variables (see blow) and then pass it through a URL it doesn’t work.. can I make this work? Or do I need to pass the array from form to form? Here is the code…[code]<html><head><title>File Upload Form</title></head><body><? if (!isset($action)) {?>This form allows you to upload a file to the server.<br><form action="getfile.php" enctype="multipart/form-data" method="post"><br>Type (or select) Filename: <input type="file" name="uploadFile"><input type="hidden" name="MAX_FILE_SIZE" value="25000" /><input type="hidden" name="action" value="fghgf" /><input type="submit" value="Upload File"></form></body></html><? } ?><? if ($action=="fghgf") {$filename1 = $_FILES['uploadFile']['name'];$filetmp1 = $_FILES['uploadFile']['tmp_name'];$filesize1 = $_FILES['uploadFile']['size'];$fileerror1 = $_FILES['uploadFile']['error'];header("Location: getfile.php?filename1=$filename1&filetmp1=$filetmp1&filesize1=$filesize1&fileerror1=$fileerror1&action=upload"); } ?><?phpif ($action=="upload") {?><html><head><title>Process Uploaded File</title></head><body><?if ( move_uploaded_file ($filetmp1, "Home/uploads/{$filename1}") ) { print '<p> The file has been successfully uploaded </p>'; }else { switch ($fileerror1) { case 1: print '<p> The file is bigger than this PHP installation allows</p>'; break; case 2: print '<p> The file is bigger than this form allows</p>'; break; case 3: print '<p> Only part of the file was uploaded</p>'; break; case 4: print '<p> No file was uploaded</p>'; break; } }?></body></html><? } ?>[/code]..Output is a blank screen with vars showing correctly in URLAny help will be much appreciated. Quote Link to comment Share on other sites More sharing options...
hitman6003 Posted August 25, 2006 Share Posted August 25, 2006 If you do not do something with the file on the page that it is uploaded to, you will not have access to it. When you upload the file it is kept in the location specified by $_FILES['uploadFile']['tmp_name'], however, when the script is done processing, that file is removed. Therefor, you must use move_uploaded_file or another function to save the file permanently.Change your form so it submits directly to action="upload", eliminating the intermediate action="fghgf". Quote Link to comment 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.