AS Posted February 25, 2010 Share Posted February 25, 2010 Here is the code I am using: ________The form <form action="upload_file.php" method="post" enctype="multipart/form-data"> <label for="file">Select Filename:</label> <input type="file" name="file" id="file" /> <input type="submit" name="submit" value="Upload File" /> </form> -----The upload_file.php <?php $target_path = "upload/"; $filename = $_FILES['file']['name']; $uploadFile = $target_path . $_FILES['file']['name']; move_uploaded_file($_FILES['file']['tm… $target_path); if(move_uploaded_file($_FILES['file'][… $target_path)) { echo "The file ". basename( $_FILES['file']['name']). " has been uploaded"; } else{ echo "There was an error uploading the file, please try again!";} ?> ______________________________________… Whenever I upload a file I get my error message "There was an error uploading the file, please try again!". I don't seem to see what's wrong with it so I thought anyone could help. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/ Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 you don't need to call move_uploaded_file twice. just in the if statement is fine. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018147 Share on other sites More sharing options...
AS Posted February 25, 2010 Author Share Posted February 25, 2010 It did not work either ways. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018151 Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 make sure you're upload directory is writable by PHP. Change it to 777 and see it if works. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018158 Share on other sites More sharing options...
AS Posted February 25, 2010 Author Share Posted February 25, 2010 AMMMMM! I don't understand what you mean, can you clarify? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018165 Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 check the file permissions on the folder you are uploading the image into. if php can't write/save to this directory it will fail. in your ftp client, get info on the folder and give that folder full permissions (777) and see if that changes anything. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018173 Share on other sites More sharing options...
AS Posted February 25, 2010 Author Share Posted February 25, 2010 Well, I just added two debug scripts one to see if the folder is not writable, and one to see if the folder does not exist. It gives a file does not exist error, even I am sure i created the folder under my server ftp? Any ideas now.? Thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018175 Share on other sites More sharing options...
schilly Posted February 25, 2010 Share Posted February 25, 2010 what code did you use? Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018176 Share on other sites More sharing options...
AS Posted February 25, 2010 Author Share Posted February 25, 2010 if (!is_dir($target_path)) { die ("Upload directory does not exists."); } if (!is_writable($target_path)) { die ("Upload directory is not writable."); } Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018178 Share on other sites More sharing options...
Deoctor Posted February 25, 2010 Share Posted February 25, 2010 <form enctype="multipart/form-data" action="upload.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" /> Choose a file to upload: <input name="uploaded" type="file" /><br /> <input type="submit" name="submit_x" value="Upload File" /> </form> <?php //Working for uploading the files of a particular type if(isset($_POST['submit_x'])) { function file_upload_error_message($error_code) { switch ($error_code) { case 0: return 'There is no error, the file uploaded with success...'; case 1: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; case 2: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; case 3: return 'The uploaded file was only partially uploaded'; case 4: return 'No file was uploaded'; case 6: return 'Missing a temporary folder'; case 7: return 'Failed to write file to disk'; case 8: return 'File upload stopped by extension'; default: return 'Unknown upload error'; } } $host="localhost"; $user="root"; $pass="admin"; $db="develop"; mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($db) or die(mysql_error()); $query="select type_name from filetype"; $result=mysql_query($query); ini_set("display_errors",1); //error_reporting(E_ALL); $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1)); print_r($_FILES['uploaded']); $error_code=$_FILES['uploaded']['error']; $error_message = file_upload_error_message($error_code); echo "Name ".$_FILES['uploaded']['name']."<br>"; echo "Type ".$_FILES['uploaded']['type']."<br>"; echo "Error ".$error_message."<br>"; echo "Size ".$_FILES['uploaded']['size']."<br>"; //echo "<br>FileX ".$uploaded_type; while($row = mysql_fetch_array($result)) { if($uploaded_type==$row['type_name']) { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; break; } else { echo "Sorry, there was a problem uploading your file."; } } } } ?> try this upload script and also check the permissions of the folder upload to be 777, this script will surely upload the files.. u need to have a mysql connecting for this to work. the table structure and the data that need to b there is like this.. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `develop`; /*Table structure for table `filetype` */ DROP TABLE IF EXISTS `filetype`; CREATE TABLE `filetype` ( `type_name` varchar(10) NOT NULL default '', PRIMARY KEY (`type_name`) ) TYPE=MyISAM; /*Data for the table `filetype` */ insert into `filetype`(`type_name`) values ('doc'),('gif'),('jpg'),('pdf'),('png'); Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018193 Share on other sites More sharing options...
AS Posted February 25, 2010 Author Share Posted February 25, 2010 No ym_chaitu, it still does not work. I created the folder with 777 but still does not copy files into it. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018260 Share on other sites More sharing options...
Deoctor Posted February 26, 2010 Share Posted February 26, 2010 Does it give u with any of the error codes that i have mentioned while uploading.. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1018355 Share on other sites More sharing options...
cdoyle Posted April 2, 2010 Share Posted April 2, 2010 Quote <form enctype="multipart/form-data" action="upload.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" /> Choose a file to upload: <input name="uploaded" type="file" /><br /> <input type="submit" name="submit_x" value="Upload File" /> </form> <?php //Working for uploading the files of a particular type if(isset($_POST['submit_x'])) { function file_upload_error_message($error_code) { switch ($error_code) { case 0: return 'There is no error, the file uploaded with success...'; case 1: return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; case 2: return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; case 3: return 'The uploaded file was only partially uploaded'; case 4: return 'No file was uploaded'; case 6: return 'Missing a temporary folder'; case 7: return 'Failed to write file to disk'; case 8: return 'File upload stopped by extension'; default: return 'Unknown upload error'; } } $host="localhost"; $user="root"; $pass="admin"; $db="develop"; mysql_connect($host,$user,$pass) or die(mysql_error()); mysql_select_db($db) or die(mysql_error()); $query="select type_name from filetype"; $result=mysql_query($query); ini_set("display_errors",1); //error_reporting(E_ALL); $target = "upload/"; $target = $target . basename( $_FILES['uploaded']['name']) ; $uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1)); print_r($_FILES['uploaded']); $error_code=$_FILES['uploaded']['error']; $error_message = file_upload_error_message($error_code); echo "Name ".$_FILES['uploaded']['name']."<br>"; echo "Type ".$_FILES['uploaded']['type']."<br>"; echo "Error ".$error_message."<br>"; echo "Size ".$_FILES['uploaded']['size']."<br>"; //echo "<br>FileX ".$uploaded_type; while($row = mysql_fetch_array($result)) { if($uploaded_type==$row['type_name']) { if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) { echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; break; } else { echo "Sorry, there was a problem uploading your file."; } } } } ?> try this upload script and also check the permissions of the folder upload to be 777, this script will surely upload the files.. u need to have a mysql connecting for this to work. the table structure and the data that need to b there is like this.. CREATE DATABASE /*!32312 IF NOT EXISTS*/ `develop`; /*Table structure for table `filetype` */ DROP TABLE IF EXISTS `filetype`; CREATE TABLE `filetype` ( `type_name` varchar(10) NOT NULL default '', PRIMARY KEY (`type_name`) ) TYPE=MyISAM; /*Data for the table `filetype` */ insert into `filetype`(`type_name`) values ('doc'),('gif'),('jpg'),('pdf'),('png'); ym_chaitu, I tried your sample code, and it seems to be working, the image is uploading. I'm just curious about it though, maybe this code isn't complete. How does the filesize, and extensions get checked before they are uploaded? I don't see anything in the code, just in the form about the value of the file size. But I don't see anything being done on the server. Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1036179 Share on other sites More sharing options...
Deoctor Posted April 5, 2010 Share Posted April 5, 2010 here i am checking for the file type $uploaded_type=strtolower(substr($_FILES['uploaded']['name'],strrpos($_FILES['uploaded']['name'],'.')+1)); and here i am comparing the type.. if($uploaded_type==$row['type_name']) the file size is here i am checking out.. <input type="hidden" name="MAX_FILE_SIZE" value="9999999999999999" /> Quote Link to comment https://forums.phpfreaks.com/topic/193383-i-need-help-with-php-file-upload-script/#findComment-1037028 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.