上传表格的正确方法

发布于 2024-10-26 04:10:16 字数 5268 浏览 1 评论 0原文

我在尝试正确上传图像时遇到了问题,我感觉这是一个基本错误。问题是它似乎没有上传图像:

addimage.php

<?php

include('includes/session.php');

include('includes/header.php');

include('includes/class/class.form.php');

include('includes/class/class.image.php');

define("MAX_SIZE","1000000");

$formImageUpload = new Form('addimage.php', 'imageUpload', 'post','multipart/form-data', ''); //return CheckImage();

$Message = "";


if(isset($_POST["submit"])) {

    $formImageUpload->setStickyData($_POST);

    $formImageUpload->checkUpload("ImagePath","image/jpeg", MAX_SIZE);


    $formImageUpload->checkNotEmpty("ImageName");


    if($formImageUpload->getValid() == true){

        $AddImage = new Image();

        $AddImage->setImageName($_POST["ImageName"]);

        $AddImage->setImagePath($_POST["ImagePath"]);

        $AddImage->imageUpload();

        $AddImage->saveImage();

        $Message = "Thank You, Your Image Has Been Uploaded";

        print_r($_FILES);

        exit;

    } else {

        $Message = "Sorry You Have An Error Please Try Again";
    }

}

$formImageUpload->openFieldset();
$formImageUpload->makeInputBox('Image','ImageName','');
$formImageUpload->makeHiddenField("MAX_FILE_SIZE", MAX_SIZE);
$formImageUpload->makeUpLoadBox('Image Path',"ImagePath",'');
$formImageUpload->makeSubmitButton("submit","Image Upload");
$formImageUpload->closeFieldset();
?>

 <section class="left">

<h1  class="Heading">Add Image</h1>

    <p class="loginError"><?php echo $Message;?></p>

<?php echo $formImageUpload->getHTML(); ?>

</section> <!-- Section Left End -->

<section class="right">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
</section><!-- Section Right End -->



<?php
include('includes/footer.php');
?>

imageclass.php

<?php

require_once('./includes/database.php');

class Image{

    private $iID;

    private $sImageName;

    private $sImagePath;

    public function loadImage($iID){

        global $database;

        $Query = "SELECT * FROM Gallery WHERE id =" .$iID;

        $resResult = $database->query($Query);

        //Fetch The Row

        $aImage = $database->fetch_array($resResult);

        $this->iID = $aImage["id"];
        $this->sImageName = $aImage["imageName"];
        $this->sImagePath = $aImage["imagePath"]; 

        $this->bExisting = true;

}

public function saveImage(){

    global $database;

    if($this->bExisting == false){

        $Query = "INSERT INTO Gallery(imageName,imagePath) VALUES ('". $database->escape_value($this->sImageName)."','".$database->escape_value($this->sImagePath)."')";

        $bResult = $database->query($Query);

        if($bResult){

            $this->iID = $database->get_last_insert_id();

            $this->bExisting = true;

        } else {

            die("Save Failed");
        }

    }
}

public function removeImage($image_id = NULL){

    // if there is no ID Exit

    if(!$image_id) return;

    global $database;

    $image_id = $database->escape_value($image_id);

    $this->loadImage($image_id);

    $database->query("DELETE FROM Gallery WHERE id = $image_id");

    if(is_file($this->image_path . $this->setImagePath)) unlink($this->image_path . $this->setImagePath);

    //Unsets Image var

    $this->_unsetImage();


}

function _unsetImage() {

    $this->iID = NULL;
    $this->sImageName = NULL;
    $this->sImagePath = NULL; 
    $this->bExisting = FALSE;

}




            // Get Functions

public function getImageID(){
        return $this->iID;
}    

public function getImageName(){
        return $this->sImageName;
}

public function getImagePath(){
        return $this->sImagePath;
}

// Set Functions

public function setImageID($iID){

    global $database;

    $iID = $database->escape_value($iID);

    $this->iID = $iID;
}    


public function setImageName($sImageName){

    global $database;

    $sImageName = $database->escape_value($sImageName);

    $this->sImageName = $sImageName;
}   

public function setImagePath($sImagePath){

    global $database;

    $sImagePath = $database->escape_value($sImagePath);

    $this->sImagePath = $sImagePath;
}


public function imageUpload($file){

    $this->bExisting = false;

    $location = base_url()."includes/images/Gallery/";

    $target = $_FILES['ImagePath']['name']; 

    $target = move_uploaded_file($_FILES ['ImagePath']['tmp_name'], $location.$target);
}



}

?>

I have had issues trying to get my images to upload correctly, I have a feeling its a basic error. The issue is that it does not seem to upload the image:

addimage.php

<?php

include('includes/session.php');

include('includes/header.php');

include('includes/class/class.form.php');

include('includes/class/class.image.php');

define("MAX_SIZE","1000000");

$formImageUpload = new Form('addimage.php', 'imageUpload', 'post','multipart/form-data', ''); //return CheckImage();

$Message = "";


if(isset($_POST["submit"])) {

    $formImageUpload->setStickyData($_POST);

    $formImageUpload->checkUpload("ImagePath","image/jpeg", MAX_SIZE);


    $formImageUpload->checkNotEmpty("ImageName");


    if($formImageUpload->getValid() == true){

        $AddImage = new Image();

        $AddImage->setImageName($_POST["ImageName"]);

        $AddImage->setImagePath($_POST["ImagePath"]);

        $AddImage->imageUpload();

        $AddImage->saveImage();

        $Message = "Thank You, Your Image Has Been Uploaded";

        print_r($_FILES);

        exit;

    } else {

        $Message = "Sorry You Have An Error Please Try Again";
    }

}

$formImageUpload->openFieldset();
$formImageUpload->makeInputBox('Image','ImageName','');
$formImageUpload->makeHiddenField("MAX_FILE_SIZE", MAX_SIZE);
$formImageUpload->makeUpLoadBox('Image Path',"ImagePath",'');
$formImageUpload->makeSubmitButton("submit","Image Upload");
$formImageUpload->closeFieldset();
?>

 <section class="left">

<h1  class="Heading">Add Image</h1>

    <p class="loginError"><?php echo $Message;?></p>

<?php echo $formImageUpload->getHTML(); ?>

</section> <!-- Section Left End -->

<section class="right">
    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry.
    Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
    It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    </p>
</section><!-- Section Right End -->



<?php
include('includes/footer.php');
?>

imageclass.php

<?php

require_once('./includes/database.php');

class Image{

    private $iID;

    private $sImageName;

    private $sImagePath;

    public function loadImage($iID){

        global $database;

        $Query = "SELECT * FROM Gallery WHERE id =" .$iID;

        $resResult = $database->query($Query);

        //Fetch The Row

        $aImage = $database->fetch_array($resResult);

        $this->iID = $aImage["id"];
        $this->sImageName = $aImage["imageName"];
        $this->sImagePath = $aImage["imagePath"]; 

        $this->bExisting = true;

}

public function saveImage(){

    global $database;

    if($this->bExisting == false){

        $Query = "INSERT INTO Gallery(imageName,imagePath) VALUES ('". $database->escape_value($this->sImageName)."','".$database->escape_value($this->sImagePath)."')";

        $bResult = $database->query($Query);

        if($bResult){

            $this->iID = $database->get_last_insert_id();

            $this->bExisting = true;

        } else {

            die("Save Failed");
        }

    }
}

public function removeImage($image_id = NULL){

    // if there is no ID Exit

    if(!$image_id) return;

    global $database;

    $image_id = $database->escape_value($image_id);

    $this->loadImage($image_id);

    $database->query("DELETE FROM Gallery WHERE id = $image_id");

    if(is_file($this->image_path . $this->setImagePath)) unlink($this->image_path . $this->setImagePath);

    //Unsets Image var

    $this->_unsetImage();


}

function _unsetImage() {

    $this->iID = NULL;
    $this->sImageName = NULL;
    $this->sImagePath = NULL; 
    $this->bExisting = FALSE;

}




            // Get Functions

public function getImageID(){
        return $this->iID;
}    

public function getImageName(){
        return $this->sImageName;
}

public function getImagePath(){
        return $this->sImagePath;
}

// Set Functions

public function setImageID($iID){

    global $database;

    $iID = $database->escape_value($iID);

    $this->iID = $iID;
}    


public function setImageName($sImageName){

    global $database;

    $sImageName = $database->escape_value($sImageName);

    $this->sImageName = $sImageName;
}   

public function setImagePath($sImagePath){

    global $database;

    $sImagePath = $database->escape_value($sImagePath);

    $this->sImagePath = $sImagePath;
}


public function imageUpload($file){

    $this->bExisting = false;

    $location = base_url()."includes/images/Gallery/";

    $target = $_FILES['ImagePath']['name']; 

    $target = move_uploaded_file($_FILES ['ImagePath']['tmp_name'], $location.$target);
}



}

?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无敌元气妹 2024-11-02 04:10:24

尝试进行测试,您只需:
var_dump($_FILES)
这是包含所有上传文件的全局数组。如果为空,请检查:

  1. 临时目录的权限
  2. 是否允许文件上传
  3. 如果您的表单元素看起来(有点)像

Try to do a test where you just:
var_dump($_FILES)
This is the global array that contains all uploaded files. If this is empty, check:

  1. Permissions on the temporary directory
  2. If file upload are allowed
  3. If your form element looks (a bit) like <form enctype="multipart/form-data">
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文