LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   php file upload (https://www.linuxquestions.org/questions/programming-9/php-file-upload-440169/)

lord-fu 04-29-2006 10:21 PM

php file upload
 
Hello,

For my first php project I am wanting to be able to upload files to MySQL. I have created the database and tables but every time I try and upload a file I recieve this error.
Code:

Warning: fread(): supplied argument is not a valid stream resource in /usr/local/www/apache22/data/start/minion/upload.php on line 4
Here is the file:
Code:

<?php
mysql_connect("localhost","DATABASEUSER","PASS");
mysql_select_db("USERDATA");
$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://here/minion/test2.php> Click Here</a>";
?>

This is the form:

Code:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<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>
</body>
</html>

Obviously the file I am trying to upload is valid as I browse to it and select it. I just am kinda stumped now.
Will gladly offer more info.
Any help is greatly appreciated.

Kethinov 04-29-2006 10:28 PM

Try not using register_globals and explicitly defining your form variables. Your $form_data variable would then become $_FILES['form_data']['tmp_name'].

lord-fu 04-29-2006 11:03 PM

Worked like a charm.
Code:

<?php
$uploadFile = $_FILES['form_data']['tmp_name'];
$uploadDesc = $_FILES['form_description']['form_description'];
$uploadName = $_FILES['file_data']['name'];
$uploadSize = $_FILES['file_data']['size'];
$uploadType = $_FILES['form_data']['type'];
mysql_connect("localhost","USER","PASS");
mysql_select_db("USERDATA");
$data = addslashes(fread(fopen($uploadFile, "r"), filesize($uploadFile)));
$result=MYSQL_QUERY("INSERT INTO uploads (description, data,filename,filesize,filetype) ". "VALUES ('$uploadDesc','$uploadFile','$uploadName','$uploadSize','$uploadType')");
$id= mysql_insert_id();
print "<p>File ID: <b>$id</b><br>";
print "<p>File Name: <b>$uploadName</b><br>";
print "<p>File Size: <b>$uploadSize</b><br>";
print "<p>File Type: <b>$uploadType</b><p>";
print "To upload another file <a href=http://here/minion/test2.php> Click Here</a>";
?>

Many thanks.


All times are GMT -5. The time now is 11:59 PM.