Jump to content

php+mysql file uploading problem


Sanjib Sinha

Recommended Posts

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.

 

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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));

 

Link to comment
Share on other sites

$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.

Link to comment
Share on other sites

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.

 

 

Link to comment
Share on other sites

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.

 

;)

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.