Reply to topic
Upload PHP file code
gilbertsavier


Joined: 25 Jun 2009
Posts: 9
Reply with quote
Hi,
I need a code that lets my customers upload a image file from their computer and then that file does 2 things

1. gets uploaded to upload folder on my server
2. a copy of that file gets e-mailed to me

this is what I have so far, any improvements/additions on this code would really be appreciated...

1.//GET IMAGE INFO
2.
3. $dir = 'images/';
4. $file_name = $_FILES['file']['name'];
5. $file_tmp = $_FILES['file']['tmp_name'];
6.
7. //CHECK FOR UPLOADED FILE
8. if(is_uploaded_file($file_tmp)){
9. //MOVE TO DIR
10. move_uploaded_file($file_tmp, $dir.$file_name);
11. }
nathacof
Forum Admin

Joined: 24 Oct 2006
Posts: 192
Location: Dover, DE
Reply with quote
http://us2.php.net/manual/en/features.file-upload.php is a great reference for this.

Look out for these, http://us2.php.net/manual/en/features.file-upload.common-pitfalls.php

Here's a good function to display the appropriate error message in the event a file upload fails:

http://us2.php.net/manual/en/features.file-upload.errors.php#90522

Depending on the size of uploaded files you might just want to email yourself a link to the file. Wink
Please help "Upload file with PHP"
ravirake


Joined: 06 Aug 2009
Posts: 1
Location: virginia
Reply with quote
Code:
<? php             

$uploaddir='path to server';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
    echo "File is valid, and was successfully uploaded.\n";
} else {
    echo "error with file upload\n";
}
?>

No matter how I change the code the file doesn't upload to the server and I have gone the above links and tried but nothing works. Please help.Sad
jamie
HostMySite Sales Rep
HostMySite Sales Rep

Joined: 19 Mar 2004
Posts: 858
Location: Newark, De
Reply with quote
If you're hosting with us send that into support@hostmysite.com and let them know what your account is. While code support is technically not their province a simple upload script should be easy - may just be a matter of file permissions on the server, pathing, etc.
nathacof
Forum Admin

Joined: 24 Oct 2006
Posts: 192
Location: Dover, DE
Reply with quote
Your $uploaddir variable needs to be set to the actual path to the directory on the server. On our shared Linux plans, the absolute path to your site's document root is:

Code:

# DocumentRoot "/vservers/<ftpusername>/htdocs";
$uploaddir = "/vservers/<ftpusername>/htdocs/uploads";


Replace <ftpusername> with the primary FTP username for your account.
Upload PHP file code
You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
All times are GMT  
Page 1 of 1  

  
  
 Reply to topic