note 46514 deleted from features.file-upload by betz

From: Date: Sat, 05 Nov 2005 09:50:06 +0000
Subject: note 46514 deleted from features.file-upload by betz
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-98117@lists.php.net to get a copy of this message
Note Submitter: dev at kiwicore dot org ---- 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']); }

« previous php.notes (#98117) next »