正在上传照片问题

时间:2011-11-04 13:16:55

标签: php image upload

我写了一个函数,从表单上传照片,在服务器上创建文件并将信息添加到数据库,但我有一个噩梦试图让它完全按照我的意愿去做。

修改

目前,如果文件类型错误,它会显示正确的错误消息,并且超过3MB但是当我尝试上传17MB .bmp文件时,它会取消并将我注销。它似乎在处理完目标函数后重新启动我的process.php文件。

我很困惑,所以任何帮助都会受到赞赏。感谢

    <form action="process.php" method="POST" enctype="multipart/form-data" name="formUpload">
        <label>Picture:</label>
        <input type="file" name="photo" id="photobrowser" tabindex="4">
        <span class="error"><?php echo $form->error("photo"); ?></span><br />
        <input type="hidden" name="sessionid" value="<?php echo $sessionid; ?>" />
        <input type="hidden" name="subphoto" value="1" />
        <input type="image" src="styling/images/button-add-photo.png" id="subBtn" tabindex="6" />
    </form>

process.php

 class Process {

function Process(){ /* Class constructor */
    global $session;

    if(isset($_POST['subphoto'])){ /* User submitted an advert photo  */
        $this->procAddPhoto();
    } else if($session->logged_in){ /* No form was submitted therefor logout */
        $this->procLogout();
    } else { /* User trying to view this file */
        header("Location: /");
    }
}
function procAddPhoto(){
    global $session, $form;

    $retval = $session->addPhoto($_FILES['photo']['size'], $_FILES['photo']['type'], $_FILES['photo']['tmp_name'], $_POST['sessionid']);    

    if($retval == 0){ /* Successful */
        // do stuff
    } else if($retval == 1){ /* Errors found */
        // do stuff
    } else if($retval == 2){ /* Adding failed */
        // do stuff
    }
   } // close function procAddPhoto()
 };

 $process = new Process; /* Initialize process */

 ?>

session.php文件

function addPhoto($subphotoSize,$subphotoType,$subphotoTmpname,$subsessionid){
    global $database, $form;

    $maxFileSize = 3000000; // bytes (3 MB)

    /* Image error checking */
    $field = "photo";
    if($subphotoSize == 0){
        $form->setError($field, "* No file selected");
    } else {
        list($width, $height, $type, $attr) = getimagesize($subphotoTmpname);
        if($width > 4000){
            $form->setError($field, "* Max photo width is 4000 pixels.");
        } else if($subphotoSize > $maxFileSize) {
            $form->setError($field, "* Photo is above the maximum of 3 MB");
        } else if( ($subphotoType != "image/jpeg") && ($subphotoType != "image/pjpeg") && ($subphotoType != "image/png") ){
            $form->setError($field, "* $subphotoType is wrong file type");
        }
    }
    /* Errors exist, have user correct them */
    if($form->num_errors > 0){
        return 1;  //Errors with form
    } else { // Else use variables

        /* Get random string for new filename name */
        $randNum = $this->generateRandStr(10);
        $filerootpath = PHOTOS_DIR.$subsessionid."/";
        $thumbrootpath = PHOTOS_DIR.$subsessionid."/thumbs/";

        if($subphotoType == "image/png"){
            $filename = $randNum.".png";
        } else if ($subphotoType == "image/jpeg" || $subphotoType == "image/pjpeg"){
            $filename = $randNum.".jpg";
        } 
        $fullURL = $filerootpath.$filename;
        $thumbURL = $thumbrootpath.$filename;

        /* Make sure file is RGB colors */
        $getimagesize = getimagesize($subphotoTmpname); 
        if (isset($getimagesize['channels']) && $getimagesize['channels'] == 4 && $getimagesize[2] == IMAGETYPE_JPEG ) { 
            $im = @imagecreatefromjpeg($subphotoTmpname); 
            if ($im) { 
                imagejpeg($im, $image, 75); 
                imagedestroy($im); 
            } 
        }


        /* Upload files to correct folders */
        move_uploaded_file($subphotoTmpname, "$fullURL");

        /* Use session ID for the advert ID because it hasnt been made yet */
        $userSession = $this->userinfo['userid'];
        $ownerID = $this->userinfo['id'];
        if(!$database->addNewPhoto($ownerID,$fullURL,$userSession,$is_main_photo, $subsessionid, $thumbURL)){
            return 2; // Failed to add to database
        }
    }
    return 0; // Success
}

2 个答案:

答案 0 :(得分:0)

只是一个想法...我猜这里有一个以上的问题。你说当有人试图通过X mb上传文件时,它基本上会击中kill开关。你知道吗,这个尺寸大小是多少?如果你这样做,你可以将它与php安装的upload_max_filesize和post_max_size设置进行比较吗?它们都应该在phpinfo()中可见。我认为默认值大约是2mb,但我可能错了。无论哪种方式,我知道当你尝试上传超出这些设置时,它基本上吐了一下。您可能必须更改该php.ini设置。

答案 1 :(得分:0)

  

如果文件类型不正确,则显示错误,除非是文件   是几MB的大小,在这种情况下,它完全记录用户,   几乎像杀死所有会话变量

请打开/etc/php5/apache2/php.ini(假设您正在使用apache2)并设置upload_max_filesize = 5M(对于5兆字节文件)并且您有file_uploads = On。然后重启apache。然后再试一次。

  

图像无法显示,因为它会导致错误。“但是,当我   从服务器下载上传的图像,显​​示图片   很好,但不是通过http。

您是否可以检查您是否有足够的权限从存储它的目录中读取图像。 (并确保您正在访问正确的目录;尝试在浏览器地址栏中复制图像的相对路径)

让我先说清楚几件事:

1.您可以上传17 MB的文件但是要限制上传超过3 MB。 2.it上传17 mb文件,但是从浏览器中你可以看到。但如果从服务器窗口下载,可以正确打开它。?

可以插入:var_dump($ ubphotoSize);的var_dump($$ maxFileSize为);在addPhot()的开头,然后再试一次。根据你似乎它无法比较文件大小。请告诉我们我们的输出。