Computing.Net > Forums > Web Development > Uploading a file using php

Uploading a file using php

Reply to Message Icon

Original Message
Name: Richard (by RichE)
Date: June 11, 2006 at 11:57:04 Pacific
Subject: Uploading a file using php
OS: Linux
CPU/Ram: 1gig/1gig
Model/Manufacturer: clone
Comment:

Hello, I am a little new to writing PHP, but stumbled my way to this point. I got a script that is supposed to add the file name to my database, and then upload the file to a specific directory. It adds it to the database fine, but does not show up in the directory it is supposed to, but says it complete sucessfully.

Any help would be appreciated. THANKS!!!

Here is the code: (Some of it - my path is correct, but I changed it in the example.)

<?

if ($action == "Load")
{
$folder = "/hsphere/local/home/user/website/images/reviews/";
@copy("$filep" , "$folder/$filep_name");
echo "
<p align=center>File $filep_name loaded...";

$result = mysql_connect("$host", "$user", "$pass") or die ("Could not save image name

Error: " . mysql_error());

@mysql_select_db("$db") or die("Could not select database");
@mysql_query("INSERT into reviews (Picture) VALUES('$filep_name')");
if($result) { echo "Image name saved into database

"; }

}

?>

<html>
<body>
<head>
<title></title>
</head>


<form action=reviewupload.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>

</body>
</html>

~Rich


Report Offensive Message For Removal

Response Number 1
Name: SN
Date: June 11, 2006 at 12:08:35 Pacific
Subject: Uploading a file using php
Reply: (edit)

First, remove all the @ characters from your commands...You shouldn't have those in there when you're developing a script, because they may be suppressing warnings and notices that will be useful in debugging.

Second, I don't see any reference here to the uploaded file...Maybe that's $file_p but I'm not sure. Check out the PHP manual article on Handling file uploads to learn how to do this bit properly.

Good luck,
-SN


Report Offensive Follow Up For Removal

Response Number 2
Name: Richard (by RichE)
Date: June 11, 2006 at 13:13:36 Pacific
Subject: Uploading a file using php
Reply: (edit)

I took out the @ symbols, but it still does not give any errors. From what I understood the $filep was the file path, but when I double checked and had it output the $filep to the screen it came out to /tmp/random characters

$filep_name is the actual filename of the file.

Something's wrong, but I'm still trying to figure out what.


I even went as far as to take the database part out, and even tried a script in an example like this one: http://www.htmlgoodies.com/beyond/php/article.php/3472551

Uploading into the same directory as the php file still does not work. I am wondering if I have to set some sort of special permissions? Or if uploading could be disabled on my server?


I'm clueless.

~Rich


Report Offensive Follow Up For Removal

Response Number 3
Name: SN
Date: June 11, 2006 at 16:14:09 Pacific
Subject: Uploading a file using php
Reply: (edit)

OK, I just went ahead and modified the script for you. Give this a go.

-SN

<?

if ($_POST["action"] == "Load")
{
$folder = "/hsphere/local/home/user/website/images/reviews/";
move_uploaded_file($_FILES["filep"]["tmp_name"] , "$folder".$_FILES["filep"]["name"]);
echo "
<p align=center>File ".$_FILES["filep"]["name"]."loaded...";

$result = mysql_connect("$host", "$user", "$pass") or die ("Could not save image name

Error: " . mysql_error());

mysql_select_db("$db") or die("Could not select database");
mysql_query("INSERT into reviews (Picture) VALUES('".$_FILES['filep']['name']."')");
if($result) { echo "Image name saved into database

"; }

}

?>

<html>
<body>
<head>
<title></title>
</head>

<form action=reviewupload.php method=post enctype="multipart/form-data">
<table border="0" cellspacing="0" align=center cellpadding="3" bordercolor="#cccccc">
<tr>
<td>File:</td>
<td><input type="file" name="filep" size=45></td>
</tr>
<tr>
<td colspan=2><p align=center>
<input type=submit name=action value="Load">
</td>
</tr>
</table>
</form>

</body>
</html>


Report Offensive Follow Up For Removal

Response Number 4
Name: Richard (by RichE)
Date: June 11, 2006 at 19:34:05 Pacific
Subject: Uploading a file using php
Reply: (edit)

Woohoo! You got it! I did have to change the permissions on that directory, but I can't believe it's done! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you! Thank you!!!!!!!!!!!!!!!!

~Rich


Report Offensive Follow Up For Removal

Response Number 5
Name: bakss
Date: August 23, 2006 at 14:23:09 Pacific
Subject: Uploading a file using php
Reply: (edit)

he's right. you can find more on google


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Uploading a file using php

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software