Sanjib Sinha Posted January 7, 2009 Share Posted January 7, 2009 I am trying to upload file from my computer to local server using php and failed. I am working with three pages : form.php, upload.php and download.php form.php is as follows: <?php // this starts the session session_start(); ?> <html> <head> <meta http-equiv="Expires" content="0"> <title>Form</title> </head> <body bgcolor="#ffffff" leftmargin="100" topmargin="100" marginwidth="100" marginheight="100"> <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="1000"> <br>File to upload:<br> <input type="file" name="form_data" size="40"> <p><input type="submit" name="submit" value="submit"> </form> </body> </html> upload.php is as follows: <?php // this starts the session session_start(); ?> <html> <head> <meta http-equiv="Expires" content="0"> <title>form Process</title> </head> <body bgcolor="#ffffff" leftmargin="100" topmargin="100" marginwidth="100" marginheight="100"> <?php mysql_connect("localhost","root",""); mysql_select_db("userfile"); echo "Uploaded"; $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=form.php> Click Here</a>"; ?> </body> </html> download.php is as follows: <?php // this starts the session session_start(); ?> <html> <head> <meta http-equiv="Expires" content="0"> <title>Downloading file</title> </head> <body bgcolor="#ffffff" leftmargin="100" topmargin="100" marginwidth="100" marginheight="100"> <?php mysql_connect("localhost","root",""); mysql_select_db("userfile"); $query = "SELECT data,filetype FROM uploads where id=$id"; $result = MYSQL_QUERY($query); $data = MYSQL_RESULT($result,0,"data"); $type = MYSQL_RESULT($result,0,"filetype"); Header( "Content-type: $type"); print $data; ?> </body> </html> Now the problem is whenever I try to upload a file (whether it's an image or text) the output is like below : Uploaded Notice: Undefined variable: form_data in C:\wamp\www\php_project-1\upload\upload.php on line 16 Notice: Undefined variable: form_data in C:\wamp\www\php_project-1\upload\upload.php on line 16 Warning: fread(): supplied argument is not a valid stream resource in C:\wamp\www\php_project-1\upload\upload.php on line 16 Notice: Undefined variable: form_description in C:\wamp\www\php_project-1\upload\upload.php on line 17 Notice: Undefined variable: form_data_name in C:\wamp\www\php_project-1\upload\upload.php on line 17 Notice: Undefined variable: form_data_size in C:\wamp\www\php_project-1\upload\upload.php on line 17 Notice: Undefined variable: form_data_type in C:\wamp\www\php_project-1\upload\upload.php on line 17 File ID: 0 Notice: Undefined variable: form_data_name in C:\wamp\www\php_project-1\upload\upload.php on line 20 File Name: Notice: Undefined variable: form_data_size in C:\wamp\www\php_project-1\upload\upload.php on line 21 File Size: Notice: Undefined variable: form_data_type in C:\wamp\www\php_project-1\upload\upload.php on line 22 File Type: To upload another file Click Here // here is the end. Where is the problem in the script? Or is it because I am trying it locally? Have a nice year everybody. Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/ Share on other sites More sharing options...
matto Posted January 7, 2009 Share Posted January 7, 2009 I think you will find you need to read from the $_FILES['form_data']['tmp_name'] varibale rather than the $form_data variable. fopen($form_data, "r") Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731306 Share on other sites More sharing options...
Sanjib Sinha Posted January 7, 2009 Author Share Posted January 7, 2009 I think you will find you need to read from the $_FILES['form_data']['tmp_name'] varibale rather than the $form_data variable. fopen($form_data, "r") Would you kindly explain a little bit? The thing is, script does not work into the remote server also. I just tried and almost the same error message came out. Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731348 Share on other sites More sharing options...
Sanjib Sinha Posted January 7, 2009 Author Share Posted January 7, 2009 When I put $_FILES['form_data']['tmp_name'] in place of (fopen($form_data, "r") it comes out: Parse error: parse error in C:\wamp\www\upload.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731411 Share on other sites More sharing options...
teng84 Posted January 7, 2009 Share Posted January 7, 2009 undefined variable means you are trying to access variables that are not variable eg.. echo $teng; should be $teng= 'yo'; echo $tent;//yo can you paste here your code where this line is C:\wamp\www\upload.php on line 6 Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731417 Share on other sites More sharing options...
Sanjib Sinha Posted January 7, 2009 Author Share Posted January 7, 2009 undefined variable means you are trying to access variables that are not variable eg.. echo $teng; should be $teng= 'yo'; echo $tent;//yo can you paste here your code where this line is C:\wamp\www\upload.php on line 6 here is the code in line 6 $data = addslashes(fread($_FILES['form_data']['tmp_name'], "r")), filesize($form_data)); Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731420 Share on other sites More sharing options...
teng84 Posted January 7, 2009 Share Posted January 7, 2009 $data = addslashes(fread($_FILES['form_data']['tmp_name'], "r")), filesize($form_data)); addslashes has only one parameter , and review the bold lines Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731422 Share on other sites More sharing options...
Sanjib Sinha Posted January 7, 2009 Author Share Posted January 7, 2009 $data = addslashes(fread($_FILES['form_data']['tmp_name'], "r")), filesize($form_data)); addslashes has only one parameter , and review the bold lines Neither way it's not working. Is it not possible to upload files to mysql database? There must be some ways. Anyway...I'll try and thanks for your concern. Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731446 Share on other sites More sharing options...
TEENFRONT Posted January 7, 2009 Share Posted January 7, 2009 Do you mean physically upload the file into the database? ? ermmmmmmm If so thats not how you go about things. use the form to upload the file to a folder on your server, then insert the link to the file into mysql, then when downloading , just grab the link from the db. Easy as pie. Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731452 Share on other sites More sharing options...
matto Posted January 7, 2009 Share Posted January 7, 2009 It is possible to store the contents of the file in the database, but what you need to do is Upload the file to the server Read the contents of the file from $_FILES['form_data']['tmp_name'] Store the contents of the file in a variable and use this in your sql insert statement. I would also change this line: $data = addslashes(fread($_FILES['form_data']['tmp_name'], "r")), filesize($form_data)); to something like this: $data = base64_encode(file_get_contents($_FILES['form_data']['tmp_name'])); Just remember to use base64_decode when pulling the file data from the database. Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-731927 Share on other sites More sharing options...
Sanjib Sinha Posted January 9, 2009 Author Share Posted January 9, 2009 It is possible to store the contents of the file in the database, but what you need to do is Upload the file to the server Read the contents of the file from $_FILES['form_data']['tmp_name'] Store the contents of the file in a variable and use this in your sql insert statement. I would also change this line: $data = addslashes(fread($_FILES['form_data']['tmp_name'], "r")), filesize($form_data)); to something like this: $data = base64_encode(file_get_contents($_FILES['form_data']['tmp_name'])); Just remember to use base64_decode when pulling the file data from the database. Thanks. I'll try this out. But The question is how could I define those variables? Should I do that or it's not needed? Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-733314 Share on other sites More sharing options...
matto Posted January 12, 2009 Share Posted January 12, 2009 You can find more info here: http://nz2.php.net/manual/en/features.file-upload.post-method.php Quote Link to comment https://forums.phpfreaks.com/topic/139788-phpmysql-file-uploading-problem/#findComment-735680 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.