Vote count:
0
I am uploading image files to a server using the following PHP file:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Subir Imagen</title>
</head>
<body>
<?php
if ((isset($_POST["enviado"])) && ($_POST["enviado"]=="form1")){
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$prefijo = $randomString;
$nombre_archivo = $_FILES['userfile']['name'];
if ($_FILES['userfile']['size']>250000){
$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";
}
$str=preg_replace('/\s+/', '', $nombre_archivo);
move_uploaded_file($_FILES['userfile']['tmp_name'],"../eventos/".$prefijo.$str);
?>
<script>
opener.document.form1.imagen_evento.value="<?php echo $prefijo.$str;?>";
self.close();
</script>
<?php
}
else {?>
<form id="form1" name="form1" method="post" action="subirimagenevento.php" data-ajax="false" enctype="multipart/form-data">
<p>
<input name="userfile" type="file" />
</p>
<p>
<input type="submit" name="button" id="button" value="Subir imagen del evento" />
<input type="hidden" name="enviado" value="form1" />
</p>
</form>
<?php }?>
</body>
</html>
I want to implement three conditions:
- The filename should not be repeated.
- The filename should not contain white spaces.
- The file size should not be greater than 250kb.
I don't want the user to upload files with filename including white spaces. That is working find.
The first condition is resolved adding a random prefix to the file name. The second condition is resolved with the included 'preg_replace' function.
But as is it now, the third condition isn't resolved.
The file is uploaded to the server without white spaces in its name and with the random prefix in its name, but the file size is not detected and the a 1MB file is uploaded without showing the warning text.
Any help is welcome.
asked 1 min ago
Uploading files to a server changing filename and limiting file size
Aucun commentaire:
Enregistrer un commentaire