I am trying to create a basic upload form for images or files. I have nearly cracked it I just need to get the actual file to be copied to the folder and it wont upload. Here is my code so far.

------------------------------- upload1.php ---------------------------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form method="post" action="upload2.php">
Upload picture: <input name="img1" type="file">
<input type="submit" name="upload" value="Upload file">
</form>
</body>
</html>

-------------------- upload2.php---------------------------
<?
// Upload file or image
$random_number = rand(1, 999999999);
$FILES['img1'] = $random_number . ".jpeg";
@copy($
FILES['img1'], "/var/www/html/testimages/". $FILES['img1']. "") or $log .= "Couldn't copy image to server<br>";
?>
<html>
</head>
<body>
<img src="<? echo $
FILES['img1']; ?>" width="260" height="150" border="0">
<p>file uploaded....</p>
</body>
</html>

    You need enctype="multipart/form-data" within your form tag, and a hidden input of MAX_FILE_SIZE set to a large number. so your form would look like:

    <form method="post" action="upload2.php" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="10000000">
    Upload picture: <input name="img1" type="file">
    <input type="submit" name="upload" value="Upload file">
    </form>
    

    Also, use the move_uploaded_file() function:

    if (move_uploaded_file($_FILES["img1"]["tmp_name"], "/var/www/html/testimages/" .  $random_number . ".jpeg")) {
    } else {
      $log .= "Couldn't copy image to server<br>";
    }
    

    You need to use the temporary filename for the file that has been uploaded, and not the filename it was originally called. The file gets uploaded to a temporary area first, so PHP needs to know what the temporary filename is to move it.

      Thats great, thank you very much. I never realised you could upload images this way.

      Now I have the basics I can extend on the code to:-

      1) Check the file type
      2) Check if the file exists
      3) Upload the image
      4) Display the image once

      5) Send an email
      6) Upload to image name to mysql

        Write a Reply...