AV1611 Posted August 20, 2007 Share Posted August 20, 2007 I spent the weekend googling this one and found a hundred scripts to do this, but none will work because they all require globals to be turned on, and I'm not about to do that I want to store some mp3's in a mysql table. I want to do it purely for the education of storing and retrieving binary data. I will use this later on other binary formats. Can someone help? Again, I do not want to turn on globals, so I don't know how to get stuff like the file type, file size, etc, on the upload. Here is a script that uses globals: <?php if ($action == "upload") { // ok, let's get the uploaded data and insert it into the db now $db = mysql_connect("localhost", "", ""); mysql_select_db("binary_files", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>"); if (isset($binFile) && $binFile != "none") { $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile))); $strDescription = addslashes(nl2br($txtDescription)); $sql = "INSERT INTO tbl_Files "; $sql .= "(description, bin_data, filename, filesize, filetype) "; $sql .= "VALUES ('$strDescription', '$data', "; $sql .= "'$binFile_name', '$binFile_size', '$binFile_type')"; $result = mysql_query($sql, $db); mysql_free_result($result); // it's always nice to clean up! echo "Thank you. The new file was successfully added to our database.<br><br>"; echo "<a href='main.php'>Continue</a>"; } mysql_close(); } else { ?> <HTML> <BODY> <FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data"> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000"> <INPUT TYPE="hidden" NAME="action" VALUE="upload"> <TABLE BORDER="1"> <TR> <TD>Description: </TD> <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD> </TR> <TR> <TD>File: </TD> <TD><INPUT TYPE="file" NAME="binFile"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD> </TR> </TABLE> </FORM> </BODY> </HTML> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/ Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 Erm.. are you talking about Super global or register globals? why would super globals be off ? (infact i didn't know you could turn superglobals off) if you mean register globals then you can use $_FILES Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328524 Share on other sites More sharing options...
keeB Posted August 20, 2007 Share Posted August 20, 2007 Just a sidenote about your script.. Not a very good idea to make <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000"> Just put it in your script. Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328532 Share on other sites More sharing options...
btherl Posted August 20, 2007 Share Posted August 20, 2007 I'm sure he means register_globals. All the data you need is available in $_REQUEST and $_FILES. Try the following: print "<pre>"; var_dump($_REQUEST); var_dump($_FILES); Put that at the top of your script and see what's inside those variables. Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328533 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 thats what i thought until Can someone help? Again, I do not want to turn on globals, so I don't know how to get stuff like the file type, file size, etc, on the upload. then a script with no globals except a super global $_FILES !! ??? maybe a misunderstanding of what register_globals is! Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328537 Share on other sites More sharing options...
AV1611 Posted August 20, 2007 Author Share Posted August 20, 2007 Yea, I guess I meant register globals .in the old days $whatever existed without doing $whatever=$_GET['whatever']; That was bad for a lot of reasons, and I it looks to me like that is what the script(s) are doing. I can use this suggestion: print "<pre>"; var_dump($_REQUEST); var_dump($_FILES); and pull the values out of the arrays. Q: The above gives this: array(3) { ["MAX_FILE_SIZE"]=> string(7) "1000000" ["action"]=> string(6) "upload" ["txtDescription"]=> string(4) "test" } array(1) { ["binFile"]=> array(5) { ["name"]=> string(14) "irishhymn1.mp3" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(2) ["size"]=> int(0) } } I still have to do this, $binFile_size=$_FILES['size']; right? The script as is says $binFile_size doesn't exist, for example. Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328575 Share on other sites More sharing options...
AV1611 Posted August 20, 2007 Author Share Posted August 20, 2007 This if (isset($_REQUEST['action'])&& $_REQUEST['action'] == "upload") { // ok, let's get the uploaded data and insert it into the db now $txtDescription=$_REQUEST['txtDescription']; $binFile_name=$_FILES['binFile']['name']; $binFile_size=$_FILES['binFile']['size']; $binFile_type=$_FILES['binFile']['type']; $db = mysql_connect("localhost", "", ""); mysql_select_db("binary_files", $db) or die(mysql_errno() . ": " . mysql_error() . "<br>"); $data = addslashes(fread(fopen($binFile_name, "r"), filesize($binFile_size))); $strDescription = addslashes(nl2br($txtDescription)); $sql = "INSERT INTO tbl_Files "; $sql .= "(description, bin_data, filename, filesize, filetype) "; $sql .= "VALUES ('$strDescription', '$data', "; $sql .= "'$binFile_name', '$binFile_size', '$binFile_type')"; $result = mysql_query($sql, $db); mysql_free_result($result); // it's always nice to clean up! echo "Thank you. The new file was successfully added to our database.<br><br>"; echo "<a href='main.php'>Continue</a>"; mysql_close(); } else { ?> <HTML> <BODY> <FORM METHOD="post" ACTION="add.php" ENCTYPE="multipart/form-data"> <INPUT TYPE="hidden" NAME="MAX_FILE_SIZE" VALUE="1000000"> <INPUT TYPE="hidden" NAME="action" VALUE="upload"> <TABLE BORDER="1"> <TR> <TD>Description: </TD> <TD><TEXTAREA NAME="txtDescription" ROWS="10" COLS="50"></TEXTAREA></TD> </TR> <TR> <TD>File: </TD> <TD><INPUT TYPE="file" NAME="binFile"></TD> </TR> <TR> <TD COLSPAN="2"><INPUT TYPE="submit" VALUE="Upload"></TD> </TR> </TABLE> </FORM> </BODY> </HTML> <?php } Gives me this: Warning: filesize() [function.filesize]: stat failed for 0 in /home/osprey/public_html/add.php on line 18 Warning: fread() [function.fread]: Length parameter must be greater than 0 in /home/osprey/public_html/add.php on line 18 When I select an mp3 from the current directory...??? Here is the var_dump for the file. It shows that the file size is 0??? (I'm on LAMP, is it a permissions thing?): array(1) { ["binFile"]=> array(5) { ["name"]=> string(14) "irishhymn1.mp3" ["type"]=> string(0) "" ["tmp_name"]=> string(0) "" ["error"]=> int(2) ["size"]=> int(0) } } Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328583 Share on other sites More sharing options...
Barand Posted August 20, 2007 Share Posted August 20, 2007 error value 2 from the manual: Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328608 Share on other sites More sharing options...
AV1611 Posted August 20, 2007 Author Share Posted August 20, 2007 error value 2 from the manual: Value: 2; The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form. what manual? what topic? Where/how did you find that? Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328892 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 the php manual php.net! Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-328894 Share on other sites More sharing options...
Barand Posted August 20, 2007 Share Posted August 20, 2007 Go to the section in the manual entitled "Handling File Uploads" (no surprises so far ) http://www.php.net/manual/en/features.file-upload.php Scroll down and click on the link "error codes" (just above example 38.3) Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-329069 Share on other sites More sharing options...
MadTechie Posted August 20, 2007 Share Posted August 20, 2007 my bad.. i thought i added that link on the php.net! :-\ Quote Link to comment https://forums.phpfreaks.com/topic/65762-solved-how-to-do-this-without-using-globals/#findComment-329071 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.