eggy790 Posted March 26, 2009 Share Posted March 26, 2009 hi everyone just registered as im stuck and hoping you guys can help, im new to php and mysql and im trying to create a page that uploads files to a database. (largest file is 100meg) but all others are below 20meg. it only has to be simple, no restrictions on what you can upload , just the feature of uploading to the database and then being abel to download them files later. ive followed this guide exactly .. pretty much using the same code. http://php.about.com/od/phpbasics/ss/mysql_files_3.htm but i get this error Warning: fread(): supplied argument is not a valid stream resource anyone know why? or have a better guide that can teach me how to have a upload section? one specific file needs to be 128mb but the others are going to be below 20mb your help is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/ Share on other sites More sharing options...
Mchl Posted March 26, 2009 Share Posted March 26, 2009 Hello Do you create a handle using fopen, and pass it to fread? It would be best if you posted your code here Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-794400 Share on other sites More sharing options...
eggy790 Posted March 26, 2009 Author Share Posted March 26, 2009 hi i will post my code later, as i dont have access to it atm, but it is pretty much the same as what is on that link.. ive just named stuff slightly different, but thats it and i get the error.. i just wanted to trial and test it first before i do it properly --- THE CODE --- table CREATE TABLE uploads (id INT(4) NOT NULL AUTO_INCREMENT PRIMARY KEY, description CHAR(50), data LONGBLOB, filename CHAR(50), filesize CHAR(50), filetype CHAR(50) ); form <form method="post" action="upload.php" enctype="multipart/form-data"> Description:<br> <input type="text" name="form_description" size="40"> <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <br>File to upload:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> <?php mysql_connect("localhost","root","admin"); mysql_select_db("practice"); $data = addslashes(fread(fopen($form_data, "r"), filesize($form_data))); $result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$form_description','$data','$form_data_name','$form_data_size','$form_data_type')"); $id= mysql_insert_id(); print "<p>File ID: <b>$id</b><br>"; print "<p>File Name: <b>$form_data_name</b><br>"; print "<p>File Size: <b>$form_data_size</b><br>"; print "<p>File Type: <b>$form_data_type</b><p>"; print "To upload another file <a href=http://localhost/upload.php> Click Here</a>"; ?> btw. am using xampp to do this if that makes any difference Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-794529 Share on other sites More sharing options...
Mchl Posted March 26, 2009 Share Posted March 26, 2009 This code relies on register_globals to be enabled (which is discouraged as it poses a security risk). You have to read data from form using $_POST variable And I doubt that you will be able to insert 100MB file without changing max_packet_size setting in MySQL Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-794533 Share on other sites More sharing options...
Stephen68 Posted March 26, 2009 Share Posted March 26, 2009 Do you need to store the file in the DB? or can you just store the files in a directory and store the link to that file in the DB? Stephen Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-794681 Share on other sites More sharing options...
eggy790 Posted March 27, 2009 Author Share Posted March 27, 2009 Do you need to store the file in the DB? or can you just store the files in a directory and store the link to that file in the DB? o wow, that is actually what i wanted to do first, just didnt know how. how would i go about doing this mate? im going to have another go on it this weekend , so as much info as you can give Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-794971 Share on other sites More sharing options...
eggy790 Posted March 27, 2009 Author Share Posted March 27, 2009 any tutorials for ti would be good Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-795086 Share on other sites More sharing options...
eggy790 Posted March 30, 2009 Author Share Posted March 30, 2009 bump, anyone able to help me with this Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-796694 Share on other sites More sharing options...
chrispos Posted March 31, 2009 Share Posted March 31, 2009 Use an upload page and a form page in PHP I have used image files below change them to what you want ie I have used gif, jpeg, png etc. If you would like to limit the file size or increase it in this value ($_FILES["file"]["size"] / 1024) change the 1024 to what you want. This is the code for the form page so just make a page with the upload html or whatever script. <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") && ($_FILES["file"]["size"] < 20000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("/your/website/directory" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "/your/website/directory" . $_FILES["file"]["name"]); echo "Stored in: " . "/your/website/directory" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } $image = $_FILES["file"]["name"]; $connect = mysql_connect connect to your database $query = "INSERT INTO `tourist`(`image`) VALUES ('$image')"; $result = mysql_query($query) or die (mysql_error()); echo "$image"; ?> This should work as your upload page best of luck from England Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-797584 Share on other sites More sharing options...
eggy790 Posted March 31, 2009 Author Share Posted March 31, 2009 thanks mate ill give it a go tomorrow Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-797777 Share on other sites More sharing options...
eggy790 Posted April 7, 2009 Author Share Posted April 7, 2009 This is really annoying me, i cant for the life of me , get it to work. i keep getting invalid file :s heres my upload code.. ive tried a simple upload with no restrictions and they say invalid file too somebody help me <?php //Include the connection details, open $connection and select database include ("connection.php"); ?> <?php if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "application/msword") || ($_FILES["file"]["type"] == "application/pdf") || ($_FILES["file"]["type"] == "application/ppt") || ($_FILES["file"]["type"] == "video/x-msvideo") && ($_FILES["file"]["size"] < 20000000000000000)) { if ($_FILES["file"]["error"] > 0) { echo "Return Code: " . $_FILES["file"]["error"] . "<br />"; } else { echo "Upload: " . $_FILES["file"]["name"] . "<br />"; echo "Type: " . $_FILES["file"]["type"] . "<br />"; echo "Size: " . ($_FILES["file"]["size"] / 10240000000000000) . " Kb<br />"; echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />"; if (file_exists("/files/" . $_FILES["file"]["name"])) { echo $_FILES["file"]["name"] . " already exists. "; } else { move_uploaded_file($_FILES["file"]["tmp_name"], "/files/" . $_FILES["file"]["name"]); echo "Stored in: " . "/files/" . $_FILES["file"]["name"]; } } } else { echo "Invalid file"; } $image = $_FILES["file"]["name"]; $application = $_FILES["file"]["name"]; $video = $_FILES["file"]["name"]; $query = "INSERT INTO `uploads`(`name`) VALUES ('$image')"; $query = "INSERT INTO `uploads`(`name`) VALUES ('$application')"; $query = "INSERT INTO `uploads`(`name`) VALUES ('$video')"; $result = mysql_query($query) or die (mysql_error()); echo "$image"; echo "$application"; echo "$video"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-803298 Share on other sites More sharing options...
PFMaBiSmAd Posted April 7, 2009 Share Posted April 7, 2009 That code is backwards. Unfortunately, it probably came from w3schools.com. They got the first example code right (it checks for an upload error first), but whoever added the ['type'] and ['size'] checking messed up and put it first, before the check for upload errors. Think about it, if the file did not upload and has an upload error, is the ['type'] or ['size'] going to be anything? That code will report an invalid file and won't ever report an upload error. And if the file is actually uploaded, the code should tell you exactly why the file is not valid. The invalid error should not combine the type/size in one test. It should tell you if the type is invalid and what the actual type was and it should tell you separately if the size was larger than allowed and what the actual size was. For troubleshooting, add the following immediately after your first opening <?php tag - echo "<pre>"; echo "POST:"; print_r($_POST); echo "FILES:"; print_r($_FILES); echo "</pre>"; Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-803441 Share on other sites More sharing options...
eggy790 Posted April 7, 2009 Author Share Posted April 7, 2009 im confused, i think i need to go back to basics, is there any tutorial running from start to finish explainign exactly what is happning and how to do this.. that fed up and bordering on missing it out, but i dont want to give in Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-803472 Share on other sites More sharing options...
eggy790 Posted April 23, 2009 Author Share Posted April 23, 2009 bump lol the site seems incomplete without it, so i want to tackle it again Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-817190 Share on other sites More sharing options...
PFMaBiSmAd Posted April 23, 2009 Share Posted April 23, 2009 http://php.net/manual/en/features.file-upload.php Quote Link to comment https://forums.phpfreaks.com/topic/151221-uploading-to-sql-database-help-php-mysql/#findComment-817322 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.