aashcool198 Posted June 13, 2009 Share Posted June 13, 2009 Hi.. i want user to upload a file which is then stored in my database i have written this code but it is not working. <?php if (isset($_GET['try'])) { $allowed_filetypes = array('.JPG','.jpg','.gif','.bmp','.png'); // These will be the types of file that will pass the validation. $max_filesize = 524288; // Maximum filesize in BYTES (currently 0.5MB). $upload_path = ''; // The place the files will be uploaded to (currently a 'files' directory). $filename = $_FILES['userfile']['name']; // Get the name of the file (including file extension). $ext = substr($filename, strpos($filename,'.'), strlen($filename)-1); // Get the extension from the filename. if(!in_array($ext,$allowed_filetypes)) die('The file you attempted to upload is not allowed.'); if(filesize($_FILES['userfile']['tmp_name']) > $max_filesize) die('The file you attempted to upload is too large.'); // Check if we can upload to the specified path, if not DIE and inform the user. // Upload the file to your specified path. if(move_uploaded_file($_FILES['userfile']['tmp_name'],$upload_path . $filename)){ echo 'Your file upload was successful, view the file <a href="' . $upload_path . $filename . '" title="Your File">here</a>'; $instr = fopen("skill.JPG","rb"); $image = addslashes(fread($instr,filesize("skill.JPG"))); $insert = mysql_query ("insert into pix (imgdata) values('$image')"); } else echo 'There was an error during the file upload. Please try again.'; } ?> <HTML> <form action="upload.php?try=true" method="post" enctype="multipart/form-data"> <p> <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br /> <button>Upload File</button> <p> </HTML> </form> Please Help! Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/ Share on other sites More sharing options...
RichardRotterdam Posted June 13, 2009 Share Posted June 13, 2009 i want user to upload a file which is then stored in my database i have written this code but it is not working. What is it that's not working? what method are you using for your form are you using the post method and are you using enctype="multipart/form-data"? <form enctype="multipart/form-data" action="uploader.php" method="post"> Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/#findComment-854943 Share on other sites More sharing options...
Quendian Posted June 13, 2009 Share Posted June 13, 2009 Your are closing your html tag before you close your form tag. Your code: <HTML> <form action="upload.php?try=true" method="post" enctype="multipart/form-data"> <p> <label for="file">Select a file:</label> <input type="file" name="userfile" id="file"> <br /> <button>Upload File</button> <p> </HTML> </form> Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/#findComment-854949 Share on other sites More sharing options...
PFMaBiSmAd Posted June 13, 2009 Share Posted June 13, 2009 The only one here who can actually troubleshoot what your code is doing on your server is you. So, what have you done to pin down what exactly it is doing, especially since you did not state what it does when you try it. Just telling us that code does not work is pointless, we already know that because you would not be witting a question about it on a programming help forum if it did work. What do you see in front of you? A blank page? The form just redisplays? A php error message? An error message from your logic? Some things to check - Have you checked if uploads are enabled on your server? Have you used a print_r() statement to display what exactly is in the $_FILES array so you know your code is receiving the data you expect? Is full php error reporting on (error_reporting set to E_ALL and display_errors set to ON) in your php.ini so that php would help you with errors it finds? Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/#findComment-854951 Share on other sites More sharing options...
aashcool198 Posted June 14, 2009 Author Share Posted June 14, 2009 The html is working correctly even the code is working partially correct. As it takes file and stores it in specified folder. But the file is not stored in database. i think there is some problem in sql command or the way i have written it. I just want to take file from user and store it in database. If someone gets any such code on net please tell me. Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/#findComment-855383 Share on other sites More sharing options...
RichardRotterdam Posted June 14, 2009 Share Posted June 14, 2009 In the following line change $image to $filename mysql_query ("insert into pix (imgdata) values('$filename')"); Quote Link to comment https://forums.phpfreaks.com/topic/162022-file-upload-to-databse/#findComment-855482 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.