PHP将4个图像上传到文件和数据库MYSQL的路径

时间:2013-12-21 15:59:20

标签: php mysql sql post

我几天来一直在反对这个问题,似乎无法在任何地方找到合适的答案。 非常感谢任何帮助!

结束目标:让用户最多上传4张图片。检查错误。使用filename将文件上传到文件系统中的users文件夹(mkdir,如果尚不存在),将文件路径存储在SQL表的相应部分。

我不断改变一些事情以获得结果的一部分,并且现在已将自己编码成一个大的车辙。 如果您有任何问题或者甚至可以建议更好的方法来达到最终目标,请告诉我。

谢谢!

SQL table: post_id, user_id, created, post_title, short_descrip, Long_descrip, image1, image1_cap, image2, image2_cap, image3, image3_cap, image4, image4_cap

<小时/> HTML:

<form method ='post' enctype='multipart/form-data' action='/posts/p_add'>
<h1><label for = 'content'>CREATE POST:</label><br></h1>
Post Title <input type='text' name='post_title'><br><br>
Short Description <input type='text' name='shortDescrip'><br><br>
Long Description<textarea name = 'longDescrip' id = 'longDescrip'></textarea>
<br><br>

<label for="image1">Image 1:</label>
<input type="file" name="image1" id="image1"><br>
Image Caption<input type='text' name='image1_cap'><br>

<label for="image2">Image 2:</label>
<input type="file" name="image2" id="image2"><br>
Image Caption<input type='text' name='image2_cap'><br>

<label for="image3">Image 3:</label>
<input type="file" name="image3" id="image3"><br>
Image Caption<input type='text' name='image3_cap'><br>

<label for="image4">Image 4:</label>
<input type="file" name="image4" id="image4"><br>
Image Caption<input type='text' name='image4_cap'><br>

<input type = 'Submit' value = 'POST'>
</form>

<小时/> 的 PHP:

public function p_add(){
        # associate post with the user
        $_POST['user_id'] = $this->user->user_id;

        # unix timestamp for created & modified
        $_POST['created'] = Time::now();
        $filename = $_POST['filename'];

        # create file path for images
        $imgPath = '/uploads/images/'.$this->user->user_id.'/';
        if (!file_exists($imgPath)){
            mkdir($imgPath, 0777, true);
        }
        if ($_FILES["file"]["error"] == 0){
                for($x=1; $x<=4 ; $x++){
                    //print_r($_FILES);
                    # upload the user-chosen file and save to img file

                    $image = Upload::upload($_FILES, 'uploads/images/', 
                        array("jpg", "JPG", "jpeg", "JPEG", "gif", "GIF", "png", "PNG"), $filename);
                    print_r($image);
                    # notify of error 
                    if($image == 'Invalid file type.') {
                        echo "invalid file type";
                    }
                    else {
                        # add to DB                
                        $data = Array("image".$x => $image);
                        DB::instance(DB_NAME)->insert('posts', $data);

                        # resize the image and save again
                        /*$imgObj = new Image($_SERVER["DOCUMENT_ROOT"].'/uploads/images/'.$image);
                        $imgObj->resize(100,100,"crop");
                        $imgObj->save_image($_SERVER["DOCUMENT_ROOT"].'/uploads/images/'.$image);*/
                    }
                }
            }else{
                # if there is an error let it be known
                echo "there has been an error"; 
            }

        # insert into db
        DB::instance(DB_NAME)->insert('posts', $_POST);

        # feedback
        echo "Your post was added";
    }

从框架上传功能:

public static function upload($file_obj, $upload_dir, $allowed_files, $new_file_name = NULL) {      

    # Access first element in file_obj array (b/c we're dealing with single file uploads only)
    $key = key($file_obj);

    $original_file_name = $file_obj[$key]['name'];
    $temp_file          = $file_obj[$key]['tmp_name'];
    $upload_dir         = $upload_dir;

    # If new file name not given, use original file name
    if($new_file_name == NULL) $new_file_name = $original_file_name;

    $file_parts  = pathinfo($original_file_name);
    $target_file = getcwd().$upload_dir . $new_file_name . "." . $file_parts['extension'];

    # Validate the filetype
    if (in_array($file_parts['extension'], $allowed_files)) {

        # Save the file
            move_uploaded_file($temp_file,$target_file);
            return $new_file_name . "." . $file_parts['extension'];

    } else {
        return 'Invalid file type.';
    }

}

1 个答案:

答案 0 :(得分:0)

indexname中有错误:

 if ($_FILES["file"])...

您没有<input name="file">尝试检查

 if ($_FILES["image1"]["error"] == 0)

并在这种情况下重写代码。

更新:

非常有趣上传功能......解决方案不好。

试试这个:

$image = Upload::upload(array($_FILES['image'.$x]), 'uploads/images/',