Alex Bridges
Alex Bridges

Reputation: 67

Uploading a file with PHP

I have been programming for years with various programming languages. I also have some experience with markup and scripting languages. I am new to PHP though. I am trying to fix my dad's website for him and I'm learning a lot as I go. I have fixed much of it but am currently stuck. A upgrade to the PHP host that my dad uses from 5.1 to 5.4 broke the website. I noticed that one of the changes is that variables needed to be defined now. I fixed a lot of the features but I am having trouble fixing the part that allows a user of the websit to upload a picture to the server. The file just isn't uploading and I'm not sure why. Here is the PHP:

$file = $_POST['file'];
$file_name = $_POST['file']['name'];
if ($file && $file_name)
{
if ($_FILES['file']['size']>$maxsize) $status = "Error: Picture size too large. Max     file size is $maxsize bytes.<br>";
if (($_FILES['file']['type']!="image/gif") && ($_FILES['file']  ['type']!="image/jpeg") && ($_FILES['file']['type']!="image/jpg") && ($_FILES['file'] ['type']!="image/png")) $status .= "Error: Wrong file type. Must be JPG or GIF only.<br>";
$picext = substr($file_name,-3); 
    $picext = strtolower($picext);
if ((!isset($status)) && ($picext!="gif") && ($picext!="jpg") && ($picext!="png"))    $status .= "Error: Wrong file type. Must be JPG or GIF or PNG only.<br>";
}

 if (!isset($status)){


//die("write".$id);

$email = $_POST['email'];
$aim = $_POST['aim'];
$icq = $_POST['icq'];
$yahoo = $_POST['yahoo'];
$homepage = $_POST['homepage'];
$myip = $_POST['myip'];
if (!$myip) $myip = $ip;
$email2 = $_POST['email2'];
$password = $_POST['password'];
$title = $_POST['title'];
$download = $_POST['download'];
$approved = $_POST['approved'];
$allowdelete = $_POST['allowdelete'];
$author = $_POST['author'];
$facebook = $_POST['facebook'];
$piclink = $_POST['piclink'];
$domain = $_POST['domain'];
$option3 = $_POST['option3'];
$secret = $_POST['secret'];
$q= "insert into $table values('', '$email', '$aim', '$icq', '$yahoo', '$homepage',      '0','0', '0', '0', '0', '0', '',        now(),'$myip','$email2','$password','$title','$download','$approved','$allowdelete','$auth              or','$facebook','$piclink','$domain','$option3','$secret')";
    $result = mysql_query($q) or die("Failed: $sql - ".mysql_error());
    $q = "select max(id) from $table";
    $result = mysql_query($q);
    $resrow = mysql_fetch_row($result);
    $id = $resrow[0];
    $picext = "png";
    $q = "update $table set picfile = '".$id.".".$picext."' where id='$id'";
    $result = mysql_query($q);
    $file = $_POST['file'];
    @copy($file, "../pics/".$id.".".$picext);
    $picfile=$id.".".$picext;
    $act = "update"; //set mode back to update
    Header("Location: newadmin.php?id=$id");

}

Here is the form html code:

<td width="91"><div align="right"><font size="-1" face="Verdana, Arial, Helvetica,           sans-serif">Upload 
    Picture:</font></div></td>
  <td width="403"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
    <form name='file' method='post' enctype="multipart/form-data" action="newadmin.php">
    <input name="file" type="file" size="50" maxlength="50">
    </form>
    </font></td>
  <td width="200">&nbsp;</td>
  <td width="18">&nbsp;</td>
</tr>
<tr> 
  <td width="91"> 
    <div align="right"><font size="-1" face="Verdana, Arial, Helvetica, sans-serif">
      <!-- update & create button -->
    <button type='submit' name='submit' value='Submit'><img src='../images/formbutton.gif' width='40' height='40' /><?echo $tag;?>

Any help would be greatly appreciated. If this helps, for some reason in the error_log $picext isn't getting set. But if I put "$picext = "png";" The file still doesn't get uploaded.

Upvotes: 1

Views: 164

Answers (2)

Alireza Fallah
Alireza Fallah

Reputation: 4607

Change first two lines to :

$file = $_FILES['file'];
$file_name = $_FILES['file']['name'];

and these lines :

$file = $_POST['file'];
@copy($file, "../pics/".$id.".".$picext);

to :

$file = $_FILES['file'];
move_uploaded_file($file['tmp_name'],"../pics/".$id.".".$picext);

Upvotes: 0

Krish R
Krish R

Reputation: 22711

Try using,

 $file = $_FILES['file']['name'];
 move_uploaded_file($_FILES['file']['tmp_name'], "../pics/".$id.".".$picext);

instead of

 $file = $_POST['file'];

Upvotes: 2

Related Questions