簡體   English   中英

上傳圖片到數據庫和文件夾

[英]upload image to database and folder

你好,這是我將圖像上傳到數據庫和文件夾的代碼,但它不起作用

這里的代碼

if ($_REQUEST[completed] == 1 || $_REQUEST[register] == 'register')
{

    $target_path = "../imagess/";

    $rand1=rand(1,1000000);
    $target_path1 = $target_path .$rand1;
    if(basename($_FILES['uploadedfile1']['name'])=="")
    {
        move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1);
        $alltarget1="imagess/".basename($_FILES['uploadedfile1']['name']);}
    else
    {
        $var1=$rand1;
        move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1);
        $alltarget1="imagess/".$var1;}<form method="post">

    <tr>
        <td>
            <input type="hidden" name="MAX_FILE_SIZE" value="10000000000" />
            <input type=hidden name=completed value=1>
            Choose a Picture to upload: [1991 x 1241] <input name="uploadedfile1" type="file" />w: 1920px | h: 503px<br /><br />
            <br/>
        </td>
    </tr>

    <tr>
        <input type="hidden"  name="register" value="register"/>
        <td><input type="submit"  value="Insert "/>
            <input type="reset"  value="Reset"/>
        </td>
    </tr>
 </form>

任何人都可以幫助我嗎

確保您的文件夾存在

../圖像/

$target_path = "../imagess/";
move_uploaded_file($_FILES['uploadedfile1']['tmp_name'], $target_path1);

並將enctype="multipart/form-data"到您的表單中,例如

<form  enctype="multipart/form-data" >
<input name="uploadedfile1" type="file">
...
</form>

檢查你的html代碼。表單代碼應該是這樣的。

<form method="post" action="" enctype="multipart/form-data">
 <tr>
  <td>
   <input type="hidden" name="MAX_FILE_SIZE" value="10000000000" />
   <input type=hidden name=completed value=1>
   Choose a Picture to upload: [1991 x 1241]
   <input name="uploadedfile1" type="file" />w: 1920px | h: 503px<br /><br />

   <br/>
   </td>
   </tr>
    <tr>
      <input type="hidden"  name="register" value="register"/>
      <td>
      <input type="submit"  value="Insert "/>
      <input type="reset"  value="Reset"/>
     </td>
     </tr>
 </form>

並檢查上傳文件夾的路徑是否正確。

 $target_path = "../imagess/";

為了通過表單包含多媒體,enctype 應該是

檢查此鏈接https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

HTML 表單

<?php
require('main.php');
if ($_POST) {
    $r = base::functionName($_POST, $_FILES);
    if ($r == 'true') {
        header("Location: your-page.php");
    }
}
?>
<form  id="thisId" method="post" enctype="multipart/form-data">
    <label>Cordinator Image <span class="required"></span></label>
            <div class="da-form-item large">
                <input type="file" name="imagepath" class="da-custom-file"/>
                <label for="imagepath" class="error" generated="true" style="display:none;"></label>
            </div>
    <div>
        <input type="submit" value="Submit">
    </div>
</form>

PHP代碼

main.php
<?php

class sql{
        static function insert($table, $values){
        // Insert PDO function 
  }
}
class base extends sql
{

    static function functionName($post,$files){
                $filename=$files['imagepath']['name'];
                if(!empty($filename)){
                    $tmp_name=$files['imagepath']['tmp_name'];
                    $rand=rand(1000,9999);
                    $dst='../imagefolder/'.$rand.$filename;
                    move_uploaded_file($tmp_name, $dst);
                    $post['imagepath']=$rand.$filename;
                }
                $r=parent::insert('tablename', $post);
                $r=is_numeric($r)?'true':'false';
                return $r;
            }
}

檢查此鏈接https://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM