使用可选的图片上传提交表单数据

时间:2014-07-09 07:39:53

标签: php forms

我有一个表单,我希望用户可以选择仅添加文本,仅添加图像或两者兼而有。

如果你在这里查看数据库表:

enter image description here

  • id:102表示填写完整表格。 (我没有错误)
  • id:121表示仅填写标题和说明。请注意.ext缺失。 (见图 1表示错误)
  • id:122表示仅上传图像。 (我没有错误)

图1错误:

Warning: getimagesize(): Filename cannot be empty in D:\htdocs\site.com\classifieds\addlisting-test.php on line 26
Your Listing has been posted!

Warning: unlink(../images/users/lea47/classifieds/53bce4ccaa93d): No such file or directory in D:\htdocs\site.com\classifieds\addlisting-test.php on line 200

第26行就在这个区块中。我将添加评论以显示第26行:

$name       =   $_FILES['image']['name'];
$temp       =   $_FILES['image']['tmp_name'];
$type       =   $_FILES['image']['type'];
$size       =   $_FILES['image']['size'];
$ext        =   strtolower(end(explode('.', $name)));
$size2      =   getimagesize($temp); //LINE 26!!!
$width  =   $size2[0];
$height     =   $size2[1];
$upload     =   uniqid();

如果有人可以帮我摆脱第一次警告,我认为会照顾取消链接警告......也许

我认为我需要在getimagesize上使用if / else语句,但说实话,它让我对如何解决这个问题感到困惑。

我的问题:

所以我要问的是,是否有人可以帮我弄清楚如何仅提交文字。没有警告消息,并在执行此操作时将upload_images列留空。类似于这个模拟id 123的photoshop。

enter image description here

此外,不是将整个脚本转储到此处,因为我正在调整图像大小并且代码有点冗长,我将其添加到pastebin中。你可以在这里找到链接。

CODE LINK

我将不胜感激任何解决方案。谢谢你的时间。

1 个答案:

答案 0 :(得分:1)

对您的脚本进行一些更改 -

更改

if (isset($_FILES['image'])) { // line : 17

if (!empty($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name'])) {

<强> [增订-1]

你能这样做吗?

$datatobeinserted = array();

if (!empty($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name'])) {
    // If you have added any image on your form field then    
    // DO the all your work here and add image into the array

        $upload_image           =       $upload_image;
        $datatobeinserted['upload_image'] = str_replace("../images/users/".escape($user->data()->username)."/classifieds/", "", $upload_image);



    // with that you can skip image upload part if you have added only text

}


$datatobeinserted['userid']                 =       $user->data()->id;
$datatobeinserted['title']                  =       Input::get('title');
$datatobeinserted['posted']                 =       date('Y-m-d H:i:s');
$datatobeinserted['description']            =       Input::get('description');

$insertdata = DB::getInstance()->insert('records', $datatobeinserted);

<强> [增订-2]

对不起延迟 -

我检查了您的完整代码,发现很多if else并且您的文字没有上传,因为您已将所有插入代码放在image_upload块中 insert没有任何其他阻止。

检查一下我有一些变化。

Pastebin

你应该这样做:

<?php
   if (!empty($_FILES['image']['name']) && !empty($_FILES['image']['tmp_name']))
   {
     // do your all image upload work here
      $datatobeinserted['image'] = $yourimagename // this should be after image upload
   }

   //Add all your data in array

    $datatobeinserted['userid']                 =       $user->data()->id;
    $datatobeinserted['title']                  =       Input::get('title');
    $datatobeinserted['posted']                 =       date('Y-m-d H:i:s');
    $datatobeinserted['description']            =       Input::get('description'); 
    $insertdata = DB::getInstance()->insert('records', $datatobeinserted);    



   if($insertedData)
    {
       echo "Added Successfully"
    } else {
      echo "Something went wrong"; 
    }
?>

插入查询不应位于任何if else内。如果你真的想这样,那么你应该检查两者(文字和图像)不应为Insert Query为空。

希望你现在能理解我想说的话。

感谢。

<强> [UPDATE-4]

以下是如何管理所有内容的方法 -

if(empty(Image name))
{
   //HERE YOU SHOULD FOR THE $_POST
   // check here for both text fields
   if(!empty($_POST['description']) && !empty($_POST['title'])
   {
      //put your all stuff here with insert query for text only
   }

} else if(!empty(Image name)) {
  // do your image stuff
}
相关问题