arbitter Posted January 4, 2010 Share Posted January 4, 2010 <?php $images = array('image/gif','image/jpeg','image/jpg','image/pjpeg'); if( in_array($_FILES['file']['type'],$images) && $_FILES['file']['size'] <= 10000000 ) { if( $_FILES['file']['error'] > 0 ) { echo 'Return Code: ',$_FILES['file']['error'],'</br >'; } else { echo 'Upload: ',$_FILES['file']['name'],'<br />'; echo 'Type: ',$_FILES['file']['type'],'<br />'; echo 'Size: ',($_FILES['file']['size'] / 1024),'KB</br >'; echo 'Temp File: ',$_FILES['file']['tmp_name'],'<br />'; $uploaddir = date("F")." '".date("y"); if( file_exists('uploads/'. $uploaddir . '/' . $_FILES['file']['name']) ) { function findexts ($_FILES) { $filename = strtolower($_FILES) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['file']['tmp_name']) ; $ran = date(dmY); $target= "uploads/"; if(file_exists($target . $uploaddir)) { $target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; } else { mkdir($target . $uploaddir); $target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; } $target3 = $target2 . $ran . $ext; move_uploaded_file($_FILES['file']['tmp_name'], $target3); } else { if(file_exists("uploads/" . $uploaddir)) { $target="uploads/" . $uploaddir; } else { mkdir("uploads/" . $uploaddir); $target="uploads/" . $uploaddir; } move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'. $uploaddir . '/' . $_FILES['file']['name']); } } } else { echo 'Ongeldig bestand.'; echo 'Kijk grootte en type na.'; } ?> Uploading a file that doesn't exist yet works perfect, but uploading a file that already exists does not work... I get: Fatal error: Cannot use string offset as an array in C:\xampp\htdocs\Depypere\uploader.php on line 34 line 34 contains "$target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; " I don't understand why it doesn't work.. I've checked everyhting 10 times, without exaggerating Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/ Share on other sites More sharing options...
ignace Posted January 4, 2010 Share Posted January 4, 2010 Try: if(file_exists($target . $uploaddir)) { var_dump($target, $uploaddir, $_FILES['file']['name']); $target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; } function findexts ($_FILES) { $filename = strtolower($_FILES) ; $exts = split("[/\\.]", $filename) ; $n = count($exts)-1; $exts = $exts[$n]; return $exts; } $ext = findexts ($_FILES['file']['tmp_name']) ; Is the same as $ext = pathinfo($_FILES['file']['tmp_name'], PATHINFO_EXTENSION); Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-988319 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 All you have to do is look at what the expected output of this code is. $ext = findexts ($_FILES['file']['tmp_name']) ; $ran = date(dmY); $target= "uploads/"; if(file_exists($target . $uploaddir)) { $target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; } else { mkdir($target . $uploaddir); $target2 = $target . $uploaddir . "/" . $_FILES['file']['name']; } $target3 = $target2 . $ran . $ext; It's going to be the original file name with the day month and year at the end. So that means that if you wanted to upload a file with the same name, you're going to have to wait till tomorrow. All you have to do to fix this is change the date at the end to microtime like this. $ran = intval(microtime(TRUE)); Now you can upload a file as long as someone else isn't uploading a file with the same filename at the exact microsecond that you are, but what are the odds of that happening any time soon? Also you should think about taking the contents of the file and sticking it in a database. It saves on lines of code, it's more secure, and i'm pretty sure its faster to read. Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-988333 Share on other sites More sharing options...
arbitter Posted January 4, 2010 Author Share Posted January 4, 2010 microtime sounds good too.. With the changes of Ignace it also works; only problem is that now I get the 'image': Image.jpgMICROTIMEtmp So I need to get that in front of the .jpg... Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-988338 Share on other sites More sharing options...
crabfinger Posted January 4, 2010 Share Posted January 4, 2010 Do some debugging, change this move_uploaded_file($_FILES['file']['tmp_name'], $target3); to this //move_uploaded_file($_FILES['file']['tmp_name'], $target3); print $target2 . '<br />' . $target3; and tell us what that gives you Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-988394 Share on other sites More sharing options...
arbitter Posted January 5, 2010 Author Share Posted January 5, 2010 But if you do that, you don't move the uploaded file do you... And indeed, the file does not get uploaded at all, I get: Upload: JorenFuifRordic.jpg Type: image/jpeg Size: 39.7275390625KB string( "uploads/" string(11) "January '10" string(19) "JorenFuifRordic.jpg" uploads/January '10/JorenFuifRordic.jpg uploads/January '10/JorenFuifRordic.jpg1262725411tmp but no file, not even the temp file, gets uploaded... Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-989162 Share on other sites More sharing options...
crabfinger Posted January 5, 2010 Share Posted January 5, 2010 But if you do that, you don't move the uploaded file do you... And indeed, the file does not get uploaded at all, I get: Upload: JorenFuifRordic.jpg Type: image/jpeg Size: 39.7275390625KB string( "uploads/" string(11) "January '10" string(19) "JorenFuifRordic.jpg" uploads/January '10/JorenFuifRordic.jpg uploads/January '10/JorenFuifRordic.jpg1262725411tmp but no file, not even the temp file, gets uploaded... That's what we want, why do we need to actually have to upload the file when we're just debugging. I've spotted quite a few discrepancies in your code. So what I did was I went ahead and slimmed it down a bit getting rid of unnecessary crap. This should meet your needs. <?php $images = array('image/gif','image/jpeg','image/jpg','image/pjpeg'); if(in_array($_FILES['file']['type'],$images) && $_FILES['file']['size'] <= 10000000 ) { if( $_FILES['file']['error'] > 0 ) { echo 'Return Code: ' . $_FILES['file']['error'] . '<br / >'; } else { echo 'Upload: ',$_FILES['file']['name'],'<br />'; echo 'Type: ',$_FILES['file']['type'],'<br />'; echo 'Size: ',($_FILES['file']['size'] / 1024),'KB</br >'; echo 'Temp File: ',$_FILES['file']['tmp_name'],'<br />'; $upload_dir = 'uploads/' . date("F") . " " . date("y") . '/'; $upload_file = pathinfo($_FILES['file']['name'],PATHINFO_FILENAME); $upload_file .= '-' . intval(microtime(TRUE)); $upload_file .= pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION); if(!file_exists($upload_dir)) { mkdir($upload_dir); } $target = $upload_dir . $upload_file; move_uploaded_file($_FILES['file']['tmp_name'],$target); } } else { echo 'Ongeldig bestand.'; echo 'Kijk grootte en type na.'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-989230 Share on other sites More sharing options...
arbitter Posted January 6, 2010 Author Share Posted January 6, 2010 Ohboy, I have a lot to learn. One problem; the file that get's uploaded is 'JorenFuifRordic-1262780889jpg', so there still needs to be a dot added. So I've added one line here: $upload_file = pathinfo($_FILES['file']['name'],PATHINFO_FILENAME); $upload_file .= '-' . intval(microtime(TRUE)); $upload_file .= '.'; $upload_file .= pathinfo($_FILES['file']['name'],PATHINFO_EXTENSION); Thanks a lot for the help, works now! Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-989629 Share on other sites More sharing options...
ignace Posted January 6, 2010 Share Posted January 6, 2010 Ohboy, I have a lot to learn. Read this http://www.scanit.be/uploads/php-file-upload.pdf Quote Link to comment https://forums.phpfreaks.com/topic/187153-small-problem-upload-script/#findComment-989647 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.