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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Uploading a file using php

Reply to Message Icon

Name: Richard (by RichE)
Date: June 11, 2006 at 11:57:04 Pacific
OS: Linux
CPU/Ram: 1gig/1gig
Product: 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



Sponsored Link
Ads by Google

Response Number 1
Name: SN
Date: June 11, 2006 at 12:08:35 Pacific
Reply:

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


0

Response Number 2
Name: Richard (by RichE)
Date: June 11, 2006 at 13:13:36 Pacific
Reply:

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


0

Response Number 3
Name: SN
Date: June 11, 2006 at 16:14:09 Pacific
Reply:

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>


0

Response Number 4
Name: Richard (by RichE)
Date: June 11, 2006 at 19:34:05 Pacific
Reply:

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


0

Response Number 5
Name: bakss
Date: August 23, 2006 at 14:23:09 Pacific
Reply:

he's right. you can find more on google


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon

banner image ? Google Search Selection



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: Uploading a file using php

problem in uploading a file www.computing.net/answers/webdevel/problem-in-uploading-a-file-/3982.html

Upload resized image in PHP www.computing.net/answers/webdevel/upload-resized-image-in-php/1502.html

String replacement problem in PHP www.computing.net/answers/webdevel/string-replacement-problem-in-php/675.html