Hi. I want to upload a file in a folder. I want to force an error loading (folder is not exist): <?php $ruta= dirname(__FILE__)."/valor2/"; $nombre_archivo = "FileOK"; $carga = move_uploaded_file($_FILES['archivo']['tmp_name'], $ruta.$nombre_archivo.'.'."csv"); if ($carga == FALSE) { $mensaje = "THERE ARE A ERROR"; } else { $mensaje = "UPLOAD OK"; } ?> PHP: But I get two messages: Warning: move_uploaded_file(C:\www/FileOK.csv): failed to open stream: No such file or directory in C:\www\UploadFile.php on line 5 HTML: and Warning: move_uploaded_file(): Unable to move 'C:\xampp\tmp\php8184.tmp' to 'C:\www/FileOK.csv' in C:\www\UploadFile.php on line 5 HTML: Why I get this messages and dont show the valur of $mensaje ?? But if my folder is OK, all run ok.
Well, first of, you never echo the $mensaje-variable, hence you won't see anything. However, you should never assume anything - never assume folder is there, and so on. Always check, and if a check fails, either ask for something from the user, or create what you expect to find. Something like this: <?php if (!is_dir(dirname(__FILE__).'/valor2/')) { mkdir(dirname(__FILE__).'/valor2/'); } $ruta = dirname(__FILE__).'/valor2/'; $nombre_archivo = "FileOK"; if (move_uploaded_file($_FILES['archivo']['tmp_name'], $ruta.$nombre_archivo.'.'."csv")) { echo "UPLOAD OK"; } else { echo "THERE IS AN ERROR"; } ?> PHP:
My code detect this: The array of the form is created, and that there are no errors. That is csv file type (application / vnd.ms-excel) and the MIME (text / plain). Check csv extension. All this because someone might change the extension of a file and put csv. The system detects that. The folder exists and it is created. Finally the file to the destination folder is transferred. Then make other processes. I want to know if the code can be optimized more to continue with the other processes that follow. Thanks. <?php $tipofile = "application/vnd.ms-excel"; $mimefile = "text/plain"; $control = FALSE; $mensaje = "ERROR"; if (isset($_FILES['archivo']) && $_FILES['archivo']['error'] === 0 ) { if(($_FILES['archivo']['type'] === $tipofile) && finfo_file(finfo_open(FILEINFO_MIME_TYPE), $_FILES['archivo']['tmp_name'])=== $mimefile) { $extension = explode(".", $_FILES['archivo']['name']); if(strtolower($extension[1]) == "csv") { $ruta = APP_BASEDIR . "/subidas/"; $nombre_archivo = "cargos"; if (!is_dir($ruta)) { mkdir(APP_BASEDIR.'/subidas/'); } if (move_uploaded_file($_FILES['archivo']['tmp_name'], $ruta.$nombre_archivo.'.'."csv")) { $control = TRUE; $mensaje = "Upload OK"; } } } } //-----------Here if the move_uploaded_file is TRUE lets go to read the rows (for example). $tpl = new Plantilla(); $tpl->assign('mensaje', $mensaje); $tpl->display('upload.tpl.php'); ?> PHP: Just in case in mi form I use size=2 (2Mb). <form name="subir_archivo" action="index.php?action=upload" method="POST" enctype="multipart/form-data"> <input type="file" name="archivo" size="2"></input> <input type="submit" value="Cargar Archivo"></input> </form> HTML:
The "size"-attribute doesn't tell the form anything about file-size limits. It has to do with size-limits on the input. So that has absolutely nothing to do there. (It has to do with for instance limiting input for a PIN-field to 4 characters (size="4")).
I want to limit the file to 2Mb. <form name="subir_archivo" action="index.php?action=upload2" method="POST" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="2097152"></input> <input type="file" name="archivo" size="2"></input> <input type="submit" value="Cargar Archivo"></input> </form> HTML: And in my php code I need to change something?
You don't modify the form - there's no way to use the form to determine size-limits. You can do it via combinations of javascript and server-side code, but the best is to just make a plain text telling people that files must be less than X bytes, and in the PHP-file, you'll have something like: $max_size = 2097152; if ($max_size < $_FILES['size']) { // do whatever } else { //file is too big } PHP:
The php manual say this: http://php.net/manual/en/features.file-upload.errors.php according to this is not necesary use $_FILES[] because MAX_FILE_SIZE in form usin the php.ini settings. What do you think?
ok, then I suppose is better to use MAX_FILE_SIZE in the form and $_FILES['size'] in the php code. Maybe is more safe in both sides (client and server).
The MAX_FILE_SIZE hidden field has nothing to do with security or anything like that. The only thing it can do is help you inform the user quicker that the file is too big. If that's not an issue, there's no point in using it.