Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

fail file upload 1

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
0
0
US
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>&nbsp;</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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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 :
[ul]
[li]Sure the file arrives to the server machine ?[/li]
[li]Sure the web server allows it to be passed to PHP ?[/li]
[li]Sure PHP has it available in [tt]upload_tmp_dir[/tt] ?[/li]
[li]Sure the file size not exceeds [tt]post_max_size[/tt] ?[/li]
[li]Sure the file size not exceeds [tt]upload_max_filesize[/tt] ?[/li]
[li]Sure $_FILES["fileToUpload"]["error"] is [tt]UPLOAD_ERR_OK[/tt] ?[/li]
[/ul]


Feherke.
feherke.ga
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Hi

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


Feherke.
feherke.ga
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
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
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
I noticed no permissions set for creating? this not cause you any problems>?>

Code:
unmask();
mkdir($upload_path . $auction_id, 0777);
 
@clearvape,

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




--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top