Upload/Download file to sql server using php (help!)
Hi can you guys help me out please? After I've finished uploading and downloading my file I get a corrupted file with a constant 87 bytes for file size even though this is not the case. Here is the upload and download code.
Location: Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
Posts: 7,686
Thanks: 42
Thanked 637 Times in 625 Posts
What column type is your "file" column? You are apparently inserting the temporary name into that column in the upload step, but then on the download step you are trying to use the "file" column as raw content...? Doesn't make any sense.
It kind of seems like you are trying to save the file to the database as a blob type, but you have no code to save the contents of the uploaded file as a blob in the database. I'd start there.
Yeah I noticed that directly after I posted but a problem still remains. Whenever I try to download the file now I get:
Warning: fopen('/usr/local/pem/vhosts/113282/tmp/phpmz6Yyk'): failed to open stream: No such file or directory in /usr/local/pem/vhosts/113282/webspace/httpdocs/show.php on line 74 Warning: fread(): supplied argument is not a valid stream resource in /usr/local/pem/vhosts/113282/webspace/httpdocs/show.php on line 75 Warning: fclose(): supplied argument is not a valid stream resource in /usr/local/pem/vhosts/113282/webspace/httpdocs/show.php on line 77 SQL Error! Query is INSERT INTO IB2FILES (name,type,size,file) VALUES (\'BrainHarmonics Insrtuctions.pdf\','application/pdf','232043',)
Error is You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\'BrainHarmonics Insrtuctions.pdf\','application/pdf','232043',)' at line 1
I think this means I don't have permissions to the tmp folder myhosting.com has given me or the file doesn't exist. I use CuteFTP to upload files to Server and it says that the tmp/ directory exists but I can't enter it because it's not there... but when I clicked GetInfo the permissions is set to owner instead of group... I am so confused. Here is my code for the upload now:
// open up the file and extract the data/content from it
$extract = fopen($file, 'r');
$content = fread($extract, $size);
$content = addslashes($content);
fclose($extract);
Warning: fopen('/usr/local/pem/vhosts/113282/tmp/phpmz6Yyk'): failed to open stream: No such file or directory in /usr/local/pem/vhosts/113282/webspace/httpdocs/show.php on line 74
I think this means I don't have permissions to the tmp folder myhosting.com has given me or the file doesn't exist.
I don't think you understand how php handles file uploads.
When you upload a file PHP temporarily stores it in the temp directory with a random file name. It is stored there for the lifetime of the script execution. In other words, when the script finishes running, PHP DELETES the file from the temp directory.
In order to save the file permanently, you have to use a function called move_uploaded_file() to move it via its temp name to your directory where you can give it whatever name you want. You can even read the file into a string using file_get_contents() and write it into a blob field in your table (remember to escape it first in case it contains any sql sensitive characters.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.
Hey thanks for the suggestions i'll try that if this next idea doesn't work. I emailed the hosting site for my web page and they sent me back an email saying I didn't have permissions so I should just create my own temporary folder and set that to the default directory. How would I go about this?
Sounds to me like your host is just giving you the brush off because they don't want to spend their time teaching you to write PHP.
As I've said, use move_uploaded_file(). This is the only way you're going to handle file uploads. You don't have to listen to this advice if you don't want to but I won't continue to advise you if you continue to ignore.
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value.