southbeach
Programmer
I normally come here after spending too much time working on code and not being able to spot the problem.
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.
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>
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.