文件上传问题

时间:2011-04-20 01:28:40

标签: php file upload

我在使用此功能时遇到问题,我似乎无法将文件转移到其中 - >当文件上传到最终位置时 - >我错过了什么:

问题功能:

  public function imageUpload(){
            $this->bExisting = false;
            $location = "includes/images/Gallery/";
            $target = $_FILES['ImagePath']['name']; 
            move_uploaded_file($_FILES['ImagePath']['tmp_name'], $location . $_FILES['ImagePath']['name'] );

  }

来源视图

    <form id='imageUpload' action='addimage.php' method= 'post' enctype='multipart/form-data'> 

<fieldset> 

<label for='ImageName'>Image:</label> 

<input type='text' name='ImageName' id='ImageName' value=''  onblur=''/> 

<input type='hidden' name='MAX_FILE_SIZE' value='1000000' /> 

<label for='ImagePath'>Image Path:</label> 

<input type='file' name='ImagePath' id='ImagePath' value='' onblur=''/><input name='submit' id = 'submit' type='submit' value='Image Upload' /> 

</fieldset> 

PHP查看:

<?php
ob_flush();
include('includes/session.php');

include('includes/header.php');

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

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


$iPageID = 1; 

if(isset($_GET["PageID"])){

    $iPageID = $_GET["PageID"];
}

$EditedPage = new Page();

$EditedPage->loadPage($iPageID);

$apageInformation = array(); // Information From DB Sticky
$apageInformation["Name"] = $EditedPage->getPageName();
$apageInformation["PageContent"] = $EditedPage->getPageContent();
$EditedPage->getPageContent();

//Sets form information

$Action = "editpage.php?PageID=".$iPageID; // Sets form file

$formEdit = new Form($Action, 'editPage','post','return CheckEditPage();','');

//Sets StickyData

$formEdit->setStickyData($apageInformation);

//Sets the Message

$Message = "";

//If it is set do below

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

    $formEdit->setStickyData($_POST);

    $formEdit->checkNotEmpty("Name");

    $formEdit->checkNotEmpty("PageContent");


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

            $EditedPage->setPageName = $database->escape_value($_POST["Name"]);

            $EditedPage->setPageContent = $database->escape_value($_POST["PageContent"]);

           $EditedPage->updatePage();

        $Message = "Page Updated";

    }else{

        $Message = "You Have An Error Mate";
    }
}


$formEdit->openFieldset();
$formEdit->makeInputBox("Name","Name","CheckInput(this.id);");
$formEdit->makeTextArea("Content", "PageContent", "20","70", "CheckInput(this.id);");
$formEdit->makeSubmitButton("submit","Edit Page");
$formEdit->closeFieldset();
?>
   <section class="left">

    <h1  class="Heading">Edit Page</h1>

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

        <?php echo $formEdit->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')?>

2 个答案:

答案 0 :(得分:0)

尝试将basename()放在第二个参数周围。

public function imageUpload(){
    $this->bExisting = false;
    $location = "includes/images/Gallery/";
    $target = $_FILES['ImagePath']['name']; 
    move_uploaded_file($_FILES['ImagePath']['tmp_name'], $location . basename($_FILES['ImagePath']['name']) );
}

此外,您的变量 $ target 未被使用。

<form enctype="multipart/form-data" action="FILEGOESHERE.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="ImagePath" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

答案 1 :(得分:0)

每当你必须将内容上传到新服务器时,我建议检查一些要点:

  • 确保chmod 777(或使其可写)您要使用的目录。 (我会首先使用777,如果它起作用,试着放一些更受限制的东西)

  • 检查phpinfo以了解正确的路径,post_max_size和upload_max_filesize。 (通常它没有把它放在html中)你必须在php.ini中更改它,或者有时你的托管服务提供商会允许你使用.htaccess

如果检查了这些点,我想也许这就是路径。所以我有时会使用它来获取与您正在使用的脚本相关的路径。它不是很干净,但对我有用:

$thepath = substr($_SERVER["SCRIPT_FILENAME"], 0, strrpos ($_SERVER["SCRIPT_FILENAME"], "/")); 
$location="$thepath/../includes/images/Gallery/"; // if it is in the parent directory of your script, for example.