Help with file uploads!

jdmaurer16

New Member
Messages
34
Reaction score
0
Points
0
Ok, so I want to have a place on my website where someone can upload files, and have them put onto the server, or possibly emailed to me. What would I use for that? I think it is some kind of PHP code, but I'm not sure. Can somebody help me out? Thanks in advance!
 
Last edited:

Starshine

Legend Killer
Messages
14,423
Reaction score
0
Points
0
I can't help you with the code, but I wanted to point out that file hosting is not allowed on x10.
 

jdmaurer16

New Member
Messages
34
Reaction score
0
Points
0
No, I do not wish to make a website with file hosting on x10. I am building a greetings card website for someone, and he wants people to be able to upload their own photos for their greeting card.
 

swamp56

New Member
Messages
3
Reaction score
0
Points
0
HTML Part:

Code:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>


Upload.php:

Code:
<?php
 
 $file_size = $_FILES['file']['size'];
 $file_name = $_FILES['file']['name'];
 $file_type = $_FILES['file']['type'];
 $random = rand(); 
 $extension = substr(strrchr($_FILES['file']['name'], "."), 1);
 $file = "upload/" . $random . "." . $extension; 
 $real_name = $file . "." . $extension;
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists, please rename the file and try again :). ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
   $file);
      echo "Stored in: <br /> <a href=\"" . $file . "\">http://yoursite.com/" . $file . "</a>";
      echo "<br /><br />(for images only)<br />HTML Code: <br /> <textarea cols=\"60\" rows=\"2\"><img src='http://yoursite.com/" . $file ."' alt='$real_name' /></textarea>";
      }
?>

That would be a basic uploading area for and the files would be stored in an uploads folder (so make one if you use that code ;) ) :) . Don't go and break the TOS though :p .
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
HTML Part:

Code:
<form method="post" action="upload.php" enctype="multipart/form-data">
<input type="file" name="file" id="file" /><br />
<input type="submit" name="submit" value="Submit" />
</form>
Upload.php:

Code:
<?php
 
 $file_size = $_FILES['file']['size'];
 $file_name = $_FILES['file']['name'];
 $file_type = $_FILES['file']['type'];
 $random = rand(); 
 $extension = substr(strrchr($_FILES['file']['name'], "."), 1);
 $file = "upload/" . $random . "." . $extension; 
 $real_name = $file . "." . $extension;
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists, please rename the file and try again :). ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
   $file);
      echo "Stored in: <br /> <a href=\"" . $file . "\">http://yoursite.com/" . $file . "</a>";
      echo "<br /><br />(for images only)<br />HTML Code: <br /> <textarea cols=\"60\" rows=\"2\"><img src='http://yoursite.com/" . $file ."' alt='$real_name' /></textarea>";
      }
?>
That would be a basic uploading area for and the files would be stored in an uploads folder (so make one if you use that code ;) ) :) . Don't go and break the TOS though :p .

PHP:
<?php

  $file_size = $_FILES['file']['size'];
 $file_name = $_FILES['file']['name'];
 $file_type = $_FILES['file']['type'];
 $random = rand(); 
 $extension = substr(strrchr($_FILES['file']['name'], "."), 1);
 $file = "upload/" . $random . "." . $extension; 
 $real_name = $file . "." . $extension;
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists, please rename the file and try again :). ";
      }
    else
      {
     if( move_uploaded_file($_FILES["file"]["tmp_name"],   $file))
    {
/*** When Uploaded Image one mail sent your email id ***/
       $to      = 'nobody@example.com';
       $subject = 'the subject';
      $message = 'hello';
      $headers = 'From: webmaster@example.com' . "\r\n" .
       'Reply-To: webmaster@example.com' . "\r\n" .
       'X-Mailer: PHP/' . phpversion();

       mail($to, $subject, $message, $headers);

    }
      echo "Stored in: <br /> <a href=\"" . $file . "\">http://yoursite.com/" . $file . "</a>";
      echo "<br /><br />(for images only)<br />HTML Code: <br /> <textarea cols=\"60\" rows=\"2\"><img src='http://yoursite.com/" . $file ."' alt='$real_name' /></textarea>";


      }
?>



Asif Khalyani
http://www.phpasks.com
 

chrisslat

New Member
Messages
6
Reaction score
0
Points
0
make sure u verify that it is an image (or whatever file type you are using), and that the dimensions (or file size) are reasonable.

this avoids uploading of harmful files, aswell as huge images that steal bandwidth.

i also recommend that you delete them after a while so no-one can use it as an image host.
 
Last edited:

jjpeacha

New Member
Messages
125
Reaction score
0
Points
0
Well an easy way to stop people using it as an image host, is to password protect the directory. You can do this by using the cpanel in your x10hosting account:

pwdprotecteg2.jpg

Or you could rename the image as it is uploaded to a temp name so that the uploader doesn't actually know what the name for the image is.

PHP:
if($_FILES['abc123']['name']!=''){
$cv_filename = "abc123_".date("sihdmY").substr($_FILES['abc123']['name'],strlen($_FILES['abc123']['name'])-4);
if(!move_uploaded_file($_FILES['abc123']['tmp_name'], "./uploads/".$abc123_filename)){
die("File " .  $_FILES['abc123']['name'] . " was not uploaded.");
}

Now look for something like this line in your form script

HTML:
<input type=file name="abc123" id="abc123" value="">

In the PHP script you will see "abc123" alot. Now look at your form script and find out what the real "id" of your upload box is called and change the "abc123" in the php script to suit your needs!

Remember to insert that script into your "uploads.php" script.

Hope this helps!

p.s if anybody notices any mistakes please say something!!
 
Last edited:

phpasks

New Member
Messages
145
Reaction score
0
Points
0
Well an easy way to stop people using it as an image host, is to password protect the directory. You can do this by using the cpanel in your x10hosting account:

pwdprotecteg2.jpg

Or you could rename the image as it is uploaded to a temp name so that the uploader doesn't actually know what the name for the image is.

PHP:
if($_FILES['abc123']['name']!=''){
$cv_filename = "abc123_".date("sihdmY").substr($_FILES['abc123']['name'],strlen($_FILES['abc123']['name'])-4);
if(!move_uploaded_file($_FILES['abc123']['tmp_name'], "./uploads/".$abc123_filename)){
die("File " .  $_FILES['abc123']['name'] . " was not uploaded.");
}
Now look for something like this line in your form script

HTML:
<input type=file name="abc123" id="abc123" value="">
In the PHP script you will see "abc123" alot. Now look at your form script and find out what the real "id" of your upload box is called and change the "abc123" in the php script to suit your needs!

Remember to insert that script into your "uploads.php" script.

Hope this helps!

p.s if anybody notices any mistakes please say something!!

Your code absolutely right. No error in your code.
Your folder permission write permisson or not..
Check it that problem only no other problem.
 

Pi606

New Member
Messages
85
Reaction score
0
Points
0
BTW, there's a nice file upload script/status bar here. It comes with a ton of features.
 
Top