简体   繁体   English

上传照片和照片尺寸

[英]upload photo and photo size

I want to protect the website from a customer can not upload pictures larger than 2MB, and if do it, receive an error designed by me. 我要保护网站免受客户无法上传大于2MB的图片的困扰,如果这样做,则会收到我设计的错误。 The code I created is this, but do not work, if the size is greater than 2mb, skip the restriction that sends him to "error.php" I try to upload the file, and displays a list of alerts 我创建的代码是这样,但不起作用,如果大小大于2mb,请跳过将其发送到“ error.php”的限制,我尝试上传文件,并显示警报列表

<?php session_start(); 
include("includes/conexiones.php");
$sql="SELECT * FROM trabajos ORDER BY id DESC LIMIT 1" ;
$resultado=mysql_query($sql);
$fila=mysql_fetch_array($resultado);
$lastid=$fila["id"];
$prefijo=$lastid+1;
if ($_POST["cserv"]!=""){
$servicio=$_POST["cserv"];}
if ($_POST["cdirv"]!=""){
$direccion=$_POST["cdirv"];}
if ($_POST["cobserv"]!=""){
$observaciones=$_POST["cobserv"];}
if ($_FILES["cfoto"]!=""){
$foto=$_FILES["cfoto"]["name"];
$nombrefoto=$prefijo.$foto;
$Upload=$_FILES["cfoto"]["name"];
$Type=$_FILES["cfoto"]["type"];
$Size=$_FILES["cfoto"]["size"];
}
if($Size > 2097152){
header("location:error.php");
}
if($Size < 2097152){
$fototmp=$_FILES["cfoto"]["tmp_name"];
list($ancho, $alto)=getimagesize($fototmp);
$nuevoancho=600;
$nuevoalto=600*$alto/$ancho;
$nuevaimg=imagecreatetruecolor($nuevoancho, $nuevoalto);
$idnuevaimg=imagecreatefromjpeg($fototmp);
imagecopyresized($nuevaimg,$idnuevaimg,0,0,0,0,$nuevoancho,$nuevoalto,$ancho,$alto);
imagejpeg ($nuevaimg,"imagenes/grandes/".$nombrefoto);
$fototmp=$_FILES["cfoto"]["tmp_name"]; 
list($ancho, $alto)=getimagesize($fototmp);
$nuevoancho=144;
$nuevoalto=144*$alto/$ancho;
$nuevaimg=imagecreatetruecolor($nuevoancho, $nuevoalto);
$idnuevaimg=imagecreatefromjpeg($fototmp);
imagecopyresized($nuevaimg,$idnuevaimg,0,0,0,0,$nuevoancho,$nuevoalto,$ancho,$alto);
imagejpeg ($nuevaimg,"imagenes/peques/".$nombrefoto);
$sql="INSERT INTO trabajos (servicio, direccion, observaciones, foto) VALUES ('$servicio',         '$direccion', '$observaciones', '$nombrefoto')";
mysql_query($sql);
$idtrabajo=mysql_insert_id();
header("location:insertartrabajo2.php?vid=$idtrabajo"); 
}
?>

if anyone has a better solution, I'm open to advice. 如果有人有更好的解决方案,我欢迎您提出建议。

thanks 谢谢

open your php.ini file, and update the parameter upload_max_filesize to 2M 打开您的php.ini文件,并将参数upload_max_filesize更新为2M

Like this: 像这样:

upload_max_filesize = 2M

save, exit the file and restart apache. 保存,退出文件并重新启动apache。

If you don't have access to php.ini, then add the following line to your .htaccess file 如果您无权访问php.ini,则将以下行添加到您的.htaccess文件中

php_value upload_max_filesize 2M

I wouldn't rely on $_FILES variable because almost every value in that variable can be overridden by malicious users. 我不会依赖$_FILES变量,因为该变量中的几乎每个值都可以被恶意用户覆盖。

When the user's upload exceeds upload_max_filesize then you just have to check in $_FILES['cfoto']['error'] . 当用户的上传内容超过upload_max_filesize您只需签入$_FILES['cfoto']['error']

Check that this value is UPLOAD_ERR_INI_SIZE to detect if the file is bigger than the max size defined in your php.ini/.htaccess: 检查此值是否为UPLOAD_ERR_INI_SIZE,以检测文件是否大于php.ini / .htaccess中定义的最大大小:

if($_FILES['cfoto']['error'] == UPLOAD_ERR_INI_SIZE) {
  // redirect to your error screen
} else {
  // proceed as normal
}

For detecting file size use this code 要检测文件大小,请使用此代码

$fileSize = $_FILES["avatar"]["size"];

and use this code for size u want... 并使用此代码获得所需的大小...

if($fileSize > 1048576) {
    header("location: ../message.php?msg=ERROR: Your image file was larger than 1mb");
    exit(); 
}

and the message.php is 和message.php是

<?php
$message = "";
$msg = preg_replace('#[^a-z 0-9.:_()]#i', '', $_GET['msg']);
if($msg == "activation_failure"){
        $message = '<h2>Activation Error</h2> Sorry there seems to have been an issue     activating your account at this time. We have already notified ourselves of this issue and  we will contact you via email when we have identified the issue.';
} else if($msg == "activation_success"){
    $message = '<h2>Activation Success</h2> Your account is now activated. <a     href="login.php">Click here to log in</a>';
} else {
    $message = $msg;
}
?>
<div><?php echo $message; ?></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM