|
|
Subject:
Uploading/downloading blobs with php/mysql
Category: Computers > Programming Asked by: apelsinen-ga List Price: $10.00 |
Posted:
10 Nov 2004 23:46 PST
Expires: 10 Dec 2004 23:46 PST Question ID: 427449 |
I need to find the errors in the provided script. I am trying to upload word documents, other documents and pictures to a mysql database using php. I run PHP ver 4.3.6 with registered globals off I can connect to the database but when I run the script i receive the following error message [error] PHP Notice: Undefined index: action in path/add.php I first thought it was a registered globals problem but have been unable to solve it. I also read on the web that " one needs to access the uploaded file information using the $_FILES[binFile] variable" and I have not been able to follow that one either - I'm a newbie as if that wasnt obvious - so basically I need to get the script to work. The script is below..it can also be found on http://www.onlamp.com/pub/a/php/2000/09/15/php_mysql.html?page=1 I might add that I dont have ti use this particular script but I thought it was a good start..my final goal is to be able to upload and download wordprocessing documents and also hopefully preview them in a browser before download..but that last part is beyond the scope o0f this question. --------begin script------ <?php if ($action == "upload") { // ok, let's get the uploaded data and insert it into the db now include "open_db.inc"; 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 } ?> ----- end script ------ |
|
There is no answer at this time. |
|
Subject:
Re: Uploading/downloading blobs with php/mysql
From: necrocyte-ga on 24 Nov 2004 00:43 PST |
I have not tested this code on my server but by reading through it I believe I have identified your problem. The issue lies within this line: $data = addslashes(fread(fopen($binFile, "r"), filesize($binFile))); Specifically, with the fopen($binFile, "r") command. Not sure exactly why, but I've just never used fopen() in that manner. There is another, simpler way. You were on the right track with $_FILES[binFile], the $_FILES array is used to store the files contents and information about the file until you use other functions to store/manipulate/display the contents. Put this at the top of your script: var_dump($_FILES); // or use print_r(); This will show you what information the array has about the uploaded file, You will end up with: $_FILES[binFile][type] // this is the filetype as header such as "text/html". You should test that the file type is one that you expected to receive from the form. If you want images only, don't accept mp3 headers. $_FILES[binFile][size] // this is simply the size of the file, in bytes. You should test that the file is not too large. $_FILES[binFile][name] // this is the name of the file (the name the uploaded gave the file, such as "my_dog_brodie.jpg") $_FILES[binFile][tmp_name] // this is the temporary name of the file, when uploaded the server saves the file in a temporary location, if you want it in your "files" directory you need to copy it over (and probably rename it). Other important functions you should be aware of when handling uploaded files: is_uploaded_file() - security http://us2.php.net/manual/en/function.is-uploaded-file.php move_uploaded_file() - used to copy the temporary file to a permanent location http://us2.php.net/manual/en/function.move-uploaded-file.php Tons more here: http://us2.php.net/manual/en/features.file-upload.php Hope this was helpful. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |