note 46514 added to features.file-upload

From: Date: Wed, 13 Oct 2004 19:58:15 +0000
Subject: note 46514 added to features.file-upload
Groups: php.notes 
Request: Send a blank email to php-notes+get-78513@lists.php.net to get a copy of this message
Becuase uploading seems to rarely work "out of the box" even if you follow the above instructions, I'll reiterate: the main reason people fail to have successful uploads, is because the directory that you are uploading to must have the right permissions, 0755 seems to work fine. I'd also like to submit my shot at an end all, be all upload capturing script. Usage : list($success,$response) = captureUpload('./user_images/',false,'_upload'); Will look inside $_FILES['upload'] for an uploaded file. The second parameter is the name of a callbaack function (false, if you don't want it), to rename the file, it will send two parameters: $file (array of file info) , $destinationDirectory It should return the name of the file. This is useful if you want to do a database lookup (for CMS integration, to see who uploaded it, etc), or do some other fancy checking. The function will also rename file "whatever_copy_1.gif" if "whatever.gif" already exists. And here is the code: function captureUpload($destDir,$nameCallback = false,$fieldName = '_upload',$maxFileSize = false){ //make sure something is there if(!isset($_FILES[$fieldName]) ||!isset($_FILES)||!is_array($_FILES[$fieldName]) ||!$_FILES[$fieldName]['name']) return array(false,'No files were uploaded. Make sure your form tag\'s enctype was set to multipart/form-data and that the right field is being checked for the uploaded file.'); //normalize the file variable $file = $_FILES[$fieldName]; if (!isset($file['type'])) $file['type'] = ''; if (!isset($file['size'])) $file['size'] = ''; if (!isset($file['tmp_name'])) $file['tmp_name'] = ''; $file['name'] = preg_replace( '/[^a-zA-Z0-9\.\$\%\'\`\-\@\{\}\~\!\#\(\)\&\_\^]/' ,'',str_replace(array(' ','%20'),array('_','_'),$file['name'])); //was it to big? if($maxFileSize && ($file['size'] > $maxFileSize)) return array(false,'The file uploaded was to large.'); //normalize destDir if(strlen($destDir)>0 && $destDir[strlen($destDir)-1] != "/") $destDir = $destDir.'/'; //should we change the filename via a callback? if($nameCallback) $file['name'] = call_user_func_array($nameCallback, array($file,$destDir)); $i = 0; //if the filename already exists, append _copy_x (with extension) if(strpos($file['name'],'.') !== false){ $bits = explode('.',$file['name']); $ext = array_pop($bits); while(file_exists($destDir.implode('.', $bits).($i?'_copy_'.$i:'').'.'.$ext)){ ++$i; $file['name'] = implode('.',$bits).($i?'_copy_'.$i:'').'.'.$ext; } //if the filename already exists, append _copy_x (no extension) } else { while(file_exists($destDir.$file['name'].($i ?'_copy_'.$i:''))){ ++$i; $file['name'] = $file['name'].($i?'_copy_'.$i:''); } } //and now the big moment if(!@copy($file['tmp_name'], $destDir.$file['name'])) return array(false,'Could not write the file "'.$file['name'].'" to: "'.$destDir.'". Permission denied.'); else return array(true,$file['name']); } ---- Manual Page -- http://www.php.net/manual/en/features.file-upload.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+46514 Delete -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46514&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+46514&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#78513) next »