Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
上载图像时出现php foreach错误_Php_Jquery_Ajax_Foreach_Image Uploading - Fatal编程技术网

上载图像时出现php foreach错误

上载图像时出现php foreach错误,php,jquery,ajax,foreach,image-uploading,Php,Jquery,Ajax,Foreach,Image Uploading,这就是问题所在。我从我的php脚本中得到一些奇怪的行为,我用它上传图像而不刷新站点。下面有两个脚本,jquery/ajax和php。实际上它工作得很好,但我发现了一个错误。事情就是这样 我选择小于5 MB的图像-图像上载-正确 我选择了一个7MB的图像-我得到一个错误“图像太大了”-正确吗 我选择小于5 MB的图像-图像上载-正确 我选择了一个7MB的映像(同一个)—我得到“为foreach提供的参数无效”和“不是映像” 当我完全按照这个顺序选择图像时,就会发生这种情况。我试着每次都选择小于5m

这就是问题所在。我从我的php脚本中得到一些奇怪的行为,我用它上传图像而不刷新站点。下面有两个脚本,jquery/ajax和php。实际上它工作得很好,但我发现了一个错误。事情就是这样

我选择小于5 MB的图像-图像上载-正确 我选择了一个7MB的图像-我得到一个错误“图像太大了”-正确吗 我选择小于5 MB的图像-图像上载-正确 我选择了一个7MB的映像(同一个)—我得到“为foreach提供的参数无效”和“不是映像”

当我完全按照这个顺序选择图像时,就会发生这种情况。我试着每次都选择小于5mb的图像。我不知道发生了什么事。请记住,我是PHP的乞丐,因此代码可能不是“应该的”,我愿意听取关于改进或采取不同做法的建议

html

PHP代码

<?php
session_start();
$sessionId = session_id();

foreach ($_FILES["userImage"]["name"] as $key => $value) {
    $name = $_FILES["userImage"]["name"][$key];
    $size = $_FILES["userImage"]["size"][$key];
    $tmpName = $_FILES["userImage"]["tmp_name"][$key];
    $type = explode("/", $_FILES["userImage"]["type"][$key]);
    $type = $type[1];
}
$possibleTypes = array("jpeg", "jpg", "gif", "png", "JPEG", "JPG");
$maxSize = 5000000;

if($size > $maxSize){
    echo "image too big";
    die();
}
else if(!in_array($type, $possibleTypes)){
    echo "not an image, this type is: " .$type;
    die();
}
else{
    $directory = ("../user_img/".$sessionId);
    if(!file_exists($directory)){
        mkdir($directory, 0777);
    }
    else{
        foreach(glob($directory ."/*.*") as $file) {
            unlink($file);
        }
    }
    move_uploaded_file( $tmpName, $directory."/" . $sessionId."_img.".$type);   

$img_src =  "user_img/".$sessionId."/".$sessionId."_img.".$type;
echo $img_src;
}
?>

您必须在
php.ini
中增加
上载最大文件大小
发布最大文件大小

似乎提供的图像太大,无法上载,因此您没有上载图像,例如,您可以在foreach中循环noh值

编辑

关于您的评论,我认为我发现了错误:

实例化
formdata=newformdata()
onChange处理程序之前

因此,在更改输入后,将img路径附加到数组中。上传完图片后,再添加下一张。但是formData数组也包含上一个数组,因此未清除。所以,当你上传新的图像而不刷新页面时,它会累加起来,所有以前上传的图像也会被传输。每次。

chmod 777都非常糟糕。那么我应该使用什么?然后你重新启动了apache?什么是
echo php_ini_get('upload_max_filesize')打印?我没有重新启动apache,在事件发布注释之前,该值设置为15 mb。。重新启动apache。不过它还是显示了15MB,因为它应该是我刚刚发出来的,多亏了你。尽管选项设置正确,但它们会以一种奇怪的方式导致问题。每次我选择一个不同的图片,它的大小会和之前的图片大小相加,当超过php.ini中的限制时(例如15mb),它会抛出一个错误。你知道我该怎么解决吗?嗯,听起来真奇怪。请求何时发送?在你选择了一张图片或者点击了一个上传按钮之后,你可以选择多张图片吗?我编辑了上面的javascript代码。第3行是触发ajax的.on(“更改”)。。。无法进行多项选择。。一次只有一个图像
if (window.FormData) {
    formdata = new FormData();
    $('#images').on("change", function(evt) {

        var reader, file;   
        file = this.files[0];

        if (window.FileReader) {
            reader = new FileReader();
            reader.readAsDataURL(file);
        }

        if (formdata) {
            formdata.append("userImage[]", file);
        }

        if (formdata) {
            $.ajax({
            url: "includes/upload.php",
            type: "POST",
            data: formdata,
            processData: false,
            contentType: false,
            success: function (res) {

                var result = res;
                if(result == "not an image"){
                    console.log("error - this is not an image");
                }else if(result == "image too big"){
                    console.log("error - image to big")
                }else{
                    console.log("result", result);
                    $('img.userImage').attr("src", result);
                }
            }
            });     
        }   

    });
}else{
    $('input[name="userImage"]').on("change", function(){
        $('input[name="submitImage"]').trigger("click");
    });
}  
<?php
session_start();
$sessionId = session_id();

foreach ($_FILES["userImage"]["name"] as $key => $value) {
    $name = $_FILES["userImage"]["name"][$key];
    $size = $_FILES["userImage"]["size"][$key];
    $tmpName = $_FILES["userImage"]["tmp_name"][$key];
    $type = explode("/", $_FILES["userImage"]["type"][$key]);
    $type = $type[1];
}
$possibleTypes = array("jpeg", "jpg", "gif", "png", "JPEG", "JPG");
$maxSize = 5000000;

if($size > $maxSize){
    echo "image too big";
    die();
}
else if(!in_array($type, $possibleTypes)){
    echo "not an image, this type is: " .$type;
    die();
}
else{
    $directory = ("../user_img/".$sessionId);
    if(!file_exists($directory)){
        mkdir($directory, 0777);
    }
    else{
        foreach(glob($directory ."/*.*") as $file) {
            unlink($file);
        }
    }
    move_uploaded_file( $tmpName, $directory."/" . $sessionId."_img.".$type);   

$img_src =  "user_img/".$sessionId."/".$sessionId."_img.".$type;
echo $img_src;
}
?>