sn1984
sn1984

Reputation: 95

File upload php

I am working on how to file upload. Its kinda confusing for me due to I am just learning this. I have a database name tblFile for the uploads. I also have a folder on my desktop name upload for the uploads. Then I have a script called filename.php. Here is a little bit of my code that I have. As you can see I most likely got some areas wrong.The uploads are not going in the uploads folder.

$aryImages=array("image/jpeg","image/png");

$aryDocs=array("application/msword","application/pdf","video/x-msvideo");

$filename=filenameSafe($_FILES['upload']['name']);

$fileType=$_FILES["upload"]["type"];

if (in_array($_FILES["upload"]["type"],$aryImages)){
    createThumb($fileType,$_FILES['uploadFile']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["uploadFile"]["type"],$aryDocs)){
    move_uploaded_file($_FILES['uploadFile']['tmp_name'],
              "/home/valerie2/public_html/elinkswap/filename.php/".$filename);

    $aryColumns=array(  "sessionID"=>$curSess,
                        "fileName"=>$filename,
                        "fileType"=>$fileType,
                        "thumbFileName"=>$thumbFilename,
                        "dateCreated"=>date('Y-m-d H:i:s'));
    dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}
else{
    echo "File Uploaded";
}

I am getting confused I have been cramping in php in all different areas into about 16 weeks and at this point everything just seems to be run ons in my head. This is a part of homework but I think i have some files backwards and I am just hoping someone will help me understand what I am doing . Thanks

Edit: Here is the file that I am working on more:

    <?php


function dbConnect(){
// Connect to the database:
 $hostname="localhost";
 $database="tblFile";
 $mysql_login="valerie2_shuawna";
 $mysql_password="norris";

 if(!($db=mysql_connect($hostname, $mysql_login, $mysql_password))){
    echo"error on connect";
 }
 else{
    if(!(mysql_select_db($database,$db))){
        echo mysql_error();
        echo "<br />error on database connection. Check your settings.";
    }
    else{
                echo "This is the home page. I have successfully made a connection to my database and everything
 is working as it should.";
        }


}

$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");
$filename=filenameSafe($_FILES['uploads']['name']);
$fileType=$_FILES["uploads"]["type"];
if (in_array($_FILES["uploads"]["type"],$aryImages)){
    createThumb($fileType,$_FILES['uploads']['tmp_name'],$filename,100,100);
}
elseif (in_array($_FILES["uploads"]["type"],$aryDocs)){
    move_uploaded_file($_FILES['uploads']['tmp_name'],
"/home/valerie2/public_html/elinkswap/uploads/".$filename);
    $aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));
    dbInsert($filename,$aryColumns,$_FILES["upload"]["type"]);
}


    else{

    echo "File Uploaded";
  }
 }
function createThumb($type,$tmpname,$filename,$new_w,$new_h){
    $thumbFilename="tmb-".$filename;
    echo $type;
    echo "<br>".$tmpname;
    if (is_numeric(strpos($type,"jpeg"))){
        $src_img=imagecreatefromjpeg($tmpname);
    }
    if (is_numeric(strpos($type,"png"))){
        $src_img=imagecreatefrompng($tmpname);
    }
    $old_x=imageSX($src_img);
    $old_y=imageSY($src_img);
    if ($old_x > $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$old_y*($new_h/$old_x);
    }
    if ($old_x < $old_y) {
        $thumb_w=$old_x*($new_w/$old_y);
        $thumb_h=$new_h;
    }
    if ($old_x == $old_y) {
        $thumb_w=$new_w;
        $thumb_h=$new_h;
    }
    $dst_img=imagecreatetruecolor($thumb_w,$thumb_h);
    imagecopyresampled($dst_img,$src_img,0,0,0,0,$thumb_w,$thumb_h,$old_x,$old_y);
    if (is_numeric(strpos($type,"jpeg"))){
        imagejpeg($dst_img,"/home/valerie2/public_html/elinkswap/uploads/".$thumbFilename);
        imagejpeg($src_img,"/home/valerie2/public_html/elinkswap/uploads/".$filename);
    }
    if (is_numeric(strpos($type,"png"))){
        imagepng($dst_img,"/home/valerie2/public_html/elinkswap/uploads/".$thumbFilename);
        imagepng($src_img,"/home/valerie2/public_html/elinkswap/uploads/".$filename);
    }
    imagedestroy($dst_img);
    imagedestroy($src_img);
    dbInsert($filename,$thumbFilename,$type);
}
function filenameSafe($filename) {
    $temp = $filename;
    // Lower case
    $temp = strtolower($temp);
    // Replace spaces with a ’_’
    $temp = str_replace(" ", "_", $temp);
    // Loop through string
    $result = "";
    for ($i=0; $i<strlen($temp); $i++) {
        if (preg_match('([0-9]|[a-z]|_|.)', $temp[$i])) {
            $result = $result.$temp[$i];
        }
    }
    dbConnect();
    $SQL="SELECT fileID FROM uploads WHERE fileName='".$result."'";
    //echo $SQL;
    $rs=mysql_query($SQL);
    echo mysql_num_rows($rs);
    if(mysql_num_rows($rs)!=0){
        $extension=strrchr($result,'.');
        $result=str_replace($extension,time(),$result);
        $result=$result.$extension;
    }
    return $result;
}

function dbInsert($filename,$thumbFilename,$type){
    dbConnect();
    $SQL="INSERT Into uploads (fileName,thumbFileName,fileType) values('".$filename."','".$thumbFilename."','".$type."')";
    //echo $SQL;
    mysql_query($SQL);


}



?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>File Upload</title>
<link href="styles.css" type="text/css" rel="stylesheet" />
</head>
<body>


<form enctype="multipart/form-data" action="filename.php" method="post">

Select File: <input type="file" name="uploads">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
<input name="Submit" type="submit" value="uploads">

</form>

But the files still dont load to the upload folder.

Upvotes: 0

Views: 520

Answers (3)

user898741
user898741

Reputation:

Well, it seems your confusing the $_FILES['upload'] and $_FILES['uploadFile'] names. I ajust all to one name: upload. Let's see the code:

First some declarations...

$aryImages=array("image/jpeg","image/png");
$aryDocs=array("application/msword","application/pdf","video/x-msvideo");

$filename=filenameSafe($_FILES['upload']['name']);
$fileType=$_FILES["upload"]["type"];

Then I think you shoul verify the file type BEFORE you generate the thumb, right? Cause if file type isn't okay, you just don't create the thumb.

if (in_array($_FILES["upload"]["type"],$aryDocs)) {

So filetype is okay, now create the thumb...

    if (in_array($_FILES["upload"]["type"],$aryImages)) {
        createThumb($fileType,$_FILES['upload']['tmp_name'],$filename,100,100);
    }

Is heavy recommended to verify all steps like the move_uploaded_file. This function is crucial to the entire script works fine...

    if (move_uploaded_file($_FILES['upload']['tmp_name'],
"/home/valerie2/public_html/elinkswap/filename.php/".$filename)) {
        $aryColumns=array("sessionID"=>$curSess,"fileName"=>$filename,"fileType"=>$fileType,"thumbFileName"=>$thumbFilename,"dateCreated"=>date('Y-m-d H:i:s'));

Good, now here you verify if your function dbInsert do his job well...

        if (dbInsert($filename,$aryColumns,$_FILES["upload"]["type"])) {
           // Upload OK
        } else {
           // Error inserting on DB
        }
    } else {
        // Error moving file!!
    }
}

Note that I have not tested this script. I hope it helps!

Upvotes: 1

user1074324
user1074324

Reputation:

In addition to what Raskolnikov wrote, also make sure your mime types cover all of what you need, for example "image/jpeg" is not the only mime type for JPG files, on some systems in can also be "image/jpg", and since the mime type is what the browser, ie) the end users machine is sending to your form, it can be one of many.

Check out http://www.webmaster-toolkit.com/mime-types.shtml for a good list.

Upvotes: 1

Steve Rukuts
Steve Rukuts

Reputation: 9367

I have noticed that you are referring to both $_FILES['upload'] and $_FILES['uploadFile']. I suspect that this is your issue. Check the name of the form field. If this does not solve your issue, I suggest reading the chapter on the PHP site about file uploads. You may also find that you have not set up your form field correctly, for example by not setting up the enctype="multipart/form-data" attribute in your <form> tag.

Also, you will get more debugging info by setting error reporting to a higher level. I suspect that this would have diagnosed your issue quicker than asking on stack overflow :)

Upvotes: 1

Related Questions