×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

fail file upload

fail file upload

fail file upload

(OP)
I normally come here after spending too much time working on code and not being able to spot the problem.

CODE

<?php
//*******************************************************************
// Rem out following two lines to avoid displaying error/warnings ...
  ini_set('display_errors', 'On');
  error_reporting(E_ALL);
//*******************************************************************

$msg=''; $saveTo=''; $subDir=''; $dir=''; $tree='./data';
if(isset($_POST['submit'])) {
    if(isset($_POST['dir']))    { if($_POST['dir'] != '')       { $dir=$_POST['dir']; } }
    if(isset($_POST['subDir'])) { if($_POST['subDir'] != '')    { $subDir=$_POST['subDir']; } }
    if($dir != '') {
        $tree=$dir;
        if(!is_dir($tree)) { echo "Created: ".$tree."<br />"; mkdir($tree,6); }
    }
    if($subDir != '') {
        $tree.='/'.$subDir; 
        if(!is_dir($tree)) { echo "<br />Created: ".$tree; mkdir($tree,6); }
    }
    $target_file = $tree . '/' . basename($_FILES["fileToUpload"]["name"]);
    $uploadOK = 1;
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
    // Check if image file is a actual image or fake image
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        $uploadOK = 1;
    } else {
        $msg.="<p>File is not an image, please try again!</p>.";
        $uploadOK = 0;
    }
    // Check file size
    if($_FILES["fileToUpload"]["size"] > 500000) {
        $msg.="<p>Sorry, your file is too large.</p>";
        $uploadOK = 0;
    } 
    // Allow certain file formats
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
        $msg.="<p>Sorry, only JPG, JPEG, PNG & GIF files are allowed.</p>";
        $uploadOK = 0;
    } 
    if($uploadOK > 0) {
        // Check if file already exists
        $randomName=randKey(25);
        $saveTo = $tree .'/'. $randomName . '.' . $imageFileType;
        echo "<br />".$saveTo;
        if(file_exists($saveTo)) {
            @unlink($saveTo);
        }
        if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $saveTo)) {
            $msg="The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
        } else {
            $msg="<p>Error Uploading chosen file, please try again."; $saveTo='';
        }
    }
}
function randKey($len)
{
    //	This function generates a random key, which can be used for temporary hyperlinks, etc
    return substr(md5(uniqid(rand(), true)), 0, $len);
}
?>
<!DOCTYPE html>
<html>
    <head>
        <?php
            if($saveTo != '') {
                echo ''
                . '<script>'
                    . 'parent.$(\'#companyLogo\').html(\'<img src="'.$saveTo.'" />\');'
                    . 'parent.$(\'#pflLogo\').val(\''.$saveTo.'\');'
                    . 'parent.$.fancybox.close();'
                . '</script>';
            }
        ?>
    </head>
    <body style="background-color: #eee;">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <p> </p>
    <input type="submit" value="Upload Chosen File" name="submit">
    <input name="dir" id="dir" value="<?php echo $_REQUEST['dir']; ?>" type="hidden" />
    <input name="id" id="id" value="<?php echo $_REQUEST['id']; ?>" type="hidden" />
    <input name="logo" id="logo" value="<?php echo $_REQUEST['logo']; ?>" type="hidden" />
    <input name="subDir" id="subDir" value="<?php if(isset($_REQUEST['subDir'])) { echo $_REQUEST['subDir']; } ?>" type="hidden" />
</form>
    <p>Please make sure to choose file type JPG, JPEG, NPG, GIF.</p>
    <div style="height: 32px; color: red; font-weight: bolder; text-align: center;"><?php echo $msg; ?></div>
</body>
</html> 
What is wrong with this code?

mkdir() were not creating directories so I created them manually - I set permissions to rw and still file is not uploaded.

I placed several echo commands and the values are shown as expected but for the life of me, I never had to write a simpler script and have so much trouble with.

thank you all for your help!


--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: fail file upload

Hi

Probably will not have time to analyze your code today, so just a brief generic list I would check, based on the issues I had in past :
  • Sure the file arrives to the server machine ?
  • Sure the web server allows it to be passed to PHP ?
  • Sure PHP has it available in upload_tmp_dir ?
  • Sure the file size not exceeds post_max_size ?
  • Sure the file size not exceeds upload_max_filesize ?
  • Sure $_FILES["fileToUpload"]["error"] is UPLOAD_ERR_OK ?

Feherke.
feherke.ga

RE: fail file upload

(OP)
Settings as per phpinfo

upload_max_filesize: 32M
post_max_size: 48M


$_FILES dump

CODE

array(1) {
  ["fileToUpload"]=>
  array(5) {
    ["name"]=>
    string(23) "hands-holding-globe.png"
    ["type"]=>
    string(9) "image/png"
    ["tmp_name"]=>
    string(14) "/tmp/phpsYoLbX"
    ["error"]=>
    int(0)
    ["size"]=>
    int(91237)
  }
} 

what say you?

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: fail file upload

Hi

Well, it works for me after
  • commenting out ini_set('display_errors', 'On') ( otherwise on first request, when $_REQUEST['dir'] is not yet set, hidden input's value will be populated with the error message )
  • created the data directory and set write permission for everybody ( my web server has no permission by default to write inside the document root )

Feherke.
feherke.ga

RE: fail file upload

(OP)
Oh, $_REQUEST['dir'] is passed alone when the script is invoked.

I use JQ

CODE

jQuery.fancybox({
            href   : 'uploadImage.php?logo=1&dir=logo&id='+id+'&subDir=miLogo',
            width  : 600,
            height : 200,
            type   :'iframe'
        }); 
to open a dialog (rendered by the original post). As you can see, all values are included within the URL string set in href:

I hate dealing with this kind of problem ... works in this computer but not on that other ... I have other routines where file(s) are uploaded without issues in the very same application and server.

Will continue to look for "root cause". Thanks Feherke!!!



--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: fail file upload

(OP)
lesson learned ... take note of relative and absolute path ...

the php script resides in a directory other than the target directory or where the image are intended to be uploaded so, fails due to "directory not found ..." but it does so silently.

this keeps me humble and makes me realize that even after having done the same type of coding 1000s of times, every now and then, "the code" will remind me that I must always pay attention.

--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

RE: fail file upload

I noticed no permissions set for creating? this not cause you any problems>?>

CODE

unmask();
mkdir($upload_path . $auction_id, 0777); 

RE: fail file upload

(OP)
@clearvape,

I think there are settings that default these already in place but to make sure, I will check. Thanks!


--
SouthBeach
http://www.fp2php.com
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close