将图像上传到数据库和文件夹

时间:2016-09-23 02:40:19

标签: php

你好这是我的代码,用于将图像上传到数据库和文件夹,但它无法正常工作

这里是代码

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>

任何人都可以帮助我

3 个答案:

答案 0 :(得分:1)

确保您的文件夹存在

  

../ imagess /

$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>

答案 1 :(得分:0)

检查你的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/";

答案 2 :(得分:0)

通过表格包含多媒体 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