6

I've built this webform wizard, consisting of several PHP pages. In this several pages users can fill in the form and the data gets temporarily stored in a session and at the last page the sessions are used to store all the data in the MYSQL database. Everything works fine with the exception of the uploaded file. Here is my code:

HTML: wizard_page2

<form name="registratieformulier" method="post" enctype="multipart/form-data" action="sw3.php">

    <tr><td>Foto winkel uploaden: </td><td><input type="file" name="uploadfoto"/></td></tr><br /><br />

      <tr><td><strong>Omschrijving van winkel:</strong></td>                    </tr><br />

       <tr><textarea cols="50" rows="7" name="omschrijvingwinkel"></textarea></tr>
       <input name="pkbedrijven" value="<?php echo($pkbedrijven); ?>" type="hidden" />
    <input type="submit" name="stuurfoto" value="Verzenden" />

</form>

PHP: wizard_last_page

    $_FILES['uploadfoto']['name']       = $_SESSION["naamfoto"];
    $_FILES['uploadfoto']['tmp_name']   = $_SESSION["tijdelijk"];

    $bn =   $_SESSION["wn"];

     $target_path = "../../winkels/$bn/";

      $target_path = $target_path . basename( $_FILES['uploadfoto']['name']);


    move_uploaded_file($_FILES['uploadfoto']['tmp_name'], $target_path)or die("There was an error uploading the file, please try again!");
     $foto_path  = "http://mywebsite.nl/winkels/$bn/".basename($_FILES['uploadfoto']['name']);


   $omschrijving   = $_SESSION["omschrijving"];

   $add = "UPDATE winkelprofiel SET winkelomschrijving='$omschrijving', winkelfoto='$foto_path' WHERE fkBedrijvenID=$pkbedrijven ";
   $query_upload = mysql_query($add) or die("De winkelfoto en omschrijving konden niet worden opgeslagen");
4

4 回答 4

9

$_FILES数组仅保存有关已在此请求中上传的文件的信息。如果您没有将该文件保存在同一请求中的其他位置,PHP 将在请求结束时将其删除。您不能简单地保存$_FILES['uploadfoto']['tmp_name']到会话中并期望文件稍后仍然存在,因为它不会存在。$_SESSION将值分配回也没有意义$_FILES,它不会带回文件。

你需要做什么:

  1. 如果上传成功,$_FILES['uploadfoto']['tmp_name']立即移动到其他地方
  2. 保存您已将其移动到的位置$_SESSION
  3. $_SESSION在多页进程结束时对该文件执行某些操作(不再需要$_FILES
  4. 有一些机制来删除旧的上传文件,以防用户放弃会话并且文件永远不会被使用
于 2012-04-26T03:23:50.700 回答
3

我认为问题在于,位于 $_FILES['uploadfoto']['tmp_name'] 的文件只有在上传时才可用。即使您将值存储在会话中,当您来到wizard_last_page 时,该文件也不会存在。您需要在 POST 请求中立即处理上传的文件。

因此,您需要在上传文件时将文件移动到 $target_path 或任何某个临时位置,然后将 $target_path 存储在会话中,以便稍后在 wizard_last_page 上访问该文件。

于 2012-04-26T03:25:50.503 回答
1

我不同意接受的答案。有一种方法可以将所有图像存储在会话数组变量中。您可以使用“file_get_contents”函数来存储图像。看看这个:

  $_SESSION['imgArrayFile'][] = $_FILES['file']; //Your file informations
  $_SESSION['imgArrayName'][] = $_POST["ImgNewNamePlacowki"]; //new name for img
  $_SESSION['ImgArrayAlt'][] = $_POST["ImgAltPlacowki"]; // alt tags if you use them
  $_SESSION['obj_image_session'][] = file_get_contents($_FILES['file']['tmp_name']); 
  //above "file_get_contents" function - store image as a long string.

无论其他人如何考虑将其用于此目的,它都可以为您完成工作。在会话中存储大量数据存在几个问题,但如果您的图像足够小并且您知道您的设置限制,那么您会很好。

保存您的文件

$file= $destination."/".$filename; //images/new.jpg
$fp=fopen($file,"w");
fwrite($fp,$_SESSION['obj_image_session'][$index]);

我(工作)项目的示例:

<?php
//$galery_img_folder = "your/new/image/destination";

foreach($_SESSION['imgArrayFile'] as $index => $name){
  if($_SESSION['imgArrayName'][$index]!="" 
  && $_SESSION['ImgArrayAlt'][$index]!=""
  && $_SESSION['obj_image_session'][$index]!=""
  ){

    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $temp = explode(".", $_SESSION['imgArrayFile'][$index]["name"]);
    $extension = end($temp);
    if ((($_SESSION['imgArrayFile'][$index]["type"] == "image/gif")
    || ($_SESSION['imgArrayFile'][$index]["type"] == "image/jpeg")
    || ($_SESSION['imgArrayFile'][$index]["type"] == "image/jpg")
    || ($_SESSION['imgArrayFile'][$index]["type"] == "image/pjpeg")
    || ($_SESSION['imgArrayFile'][$index]["type"] == "image/x-png")
    || ($_SESSION['imgArrayFile'][$index]["type"] == "image/png"))
    && ($_SESSION['imgArrayFile'][$index]["size"] < 104857600)
    && in_array($extension, $allowedExts))
      {
          if(isset($_SESSION['imgArrayName'][$index]) && $_SESSION['imgArrayName'][$index]!=""){ 
          $rename = $_SESSION['imgArrayName'][$index];
          $rename = $rename.".".end($temp);
        }

      if ($_SESSION['imgArrayFile'][$index]["error"] > 0)
        {
        echo "Return Error Code: " . $_SESSION['imgArrayFile'][$index]["error"] . "<br>";
        }
      else
        {
        $size = display_filesize($_SESSION['imgArrayFile'][$index]["size"]);
        echo "Upload: " . $_SESSION['imgArrayFile'][$index]["name"] . "<br>";
        echo "Type: " . $_SESSION['imgArrayFile'][$index]["type"] . "<br>";
        echo "Size: " . ($size) . "<br>";
        echo "Temp file: " . $_SESSION['imgArrayFile'][$index]["tmp_name"] . "<br>";

        if (file_exists($galery_img_folder."/".$rename))
          {
          $error[] = ''.$rename.' <span class="error" id="error"> this name exsists </span>';
          }
        else
          {
          $_FILES["file"]["tmp_name"]=$_SESSION['imgArrayFile'][$index]["tmp_name"];
          move_uploaded_file($_FILES["file"]["tmp_name"], $galery_img_folder."/".$rename);
          //now make use of the file_get_content variables
          $file= $galery_img_folder."/".$rename;
          $fp=fopen($file,"w");
          fwrite($fp,$_SESSION['obj_image_session'][$index]);
        }
          }
        }
      }
    else
      {
      $error[] = '<span class="error" id="error"> Niewłaściwy plik </span>';
      $maxsixe = display_filesize(104857600);
      echo "Size: " . ($maxsixe) . "<br>";
      }
    }
  }//end foreach ! ! ! 
}//end dodawanie zdjecia
?>

当然,您必须进行一些小的修改才能使其适用于您的项目,但我的意思是向您展示这是可能的。

祝你有美好的一天,快乐的编码!

于 2014-02-10T22:16:46.347 回答
1

好吧,您可以先将文件上传到一个临时位置。然后在最后一页,提交表单后,您可以将文件传输到所需位置,然后删除临时文件。

$_SESSION['file'] = $_FILES["file"]["name"];

if (file_exists("uploads/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],"uploads/temp/" . $_FILES["file"]["name"]);
      };


//This for the last page.
$file = file_get_contents("uploads/temp/".$_SESSION['file']);
file_put_contents("uploads/".$_SESSION['file'], $file);
于 2014-01-30T02:03:02.433 回答