上传图像时出错


Error uploading image

这对我自己来说是一个相当沉重的脚本,相当先进,但到目前为止,我的慈善机构网站进展良好!所有工作正常,但是我最近实现了上传文件,然后我希望将文件位置存储到我的数据库中。

以下PHP代码是我到目前为止所拥有的:

<?php
    if (isset($_POST['submitted'] ) ) {
        $host_name  = "dbXXXXXXXX";
        $database   = "dbXXXXXXXX";
        $user_name  = "dbXXXXXXXX";
        $password   = "XXXXXXXXXX";
        $db = mysqli_connect( $host_name, $user_name, $password, $database );   
        if (!$db)   
        {   
            die("Failed to connect to MySQL: " . mysql_error());   
        }
        // example of inserting data into that table: 
        $sql = "INSERT INTO BananzaNews(title,newsDate,imagePath,content,websitePath) " 
             . " VALUES( ?, NOW(), ?, ?, ? )"; 
        $stmt = $db->prepare( $sql ); 
        if (!$stmt) 
        { 
            die("Failed to prepare statement: " . $sql); 
        } 
        // here I only get the title, website, and summary from the <form> 
        // that posted to this page: 
        $title = $_POST["news_title"]; 
        $desc  = $_POST["news_content"];
        $more  = $_POST["news_websitePath"];
        if (!empty($_FILES["file"])) {
            $allowedExt = array("png","jpg","jpeg","gif");
            //echo $temp = explode(".",$_FILES["file"]["name"]);
            $extension = end($temp);
            //echo "uploading...";
            if ($_FILES["file"]["error"] > 0)
            {
                die("An error occurred uploading ".$_FILES["file"]["error"].": ".$sql);
            }
                else
            {
            move_uploaded_file($_FILES["file"]["tmp_name"],
                "Desktop/IMG/BananzaNews/Thumbs/" . $_FILES["file"]["name"]);
                $img = "Desktop/IMG/BananzaNews/Thumbs/" . $_FILES["file"]["name"];
            }
        } else {
            die("An error occurred uploading ".$_FILES["file"]["error"].": ".$sql);
        }
        $stmt->bind_param("ssss", $title, $img, $desc, $more ); 
        if ( ! $stmt->execute() ) 
        { 
            die("Execution of bound statement failed: " . $stmt->error); 
        } 
        echo "Inserted {$stmt->affected_rows} correctly.<hr/>"; 
        $db->close();
    }
?>

显示的错误消息:

上传时出错:插入到 BananzaNews(title,newsDate,imagePath,content,websitePath) VALUES( ?, 现在(), ?, ?, ?)

另外,我应该注意,由于此页面位于 admin.website 上,我已经指定了完整路径,我只想将其放在主站点上。

请求的附加

网页格式代码:

<form class="Form" action="index.php" method="post">
    <div class="Container">
        <div class="Row">
            <!--<label>Enter the RaffleBananza news title.</label>-->
            <input type="text" name="news_title" id="news_title" placeholder="Enter News Title">
        </div>
        <div class="ClearFix"></div>
        <div class="Row">
            <!--<label>Upload a cover image to be shown on preview mode.</label>-->
            <div id="file">Choose file</div>
            <input type="file" name="file" />
        </div>
        <div class="ClearFix"></div>
        <div class="Row">
            <!--<label>Enter the short description seen on the preview mode.</label>-->
            <textarea rows="5" cols="40" name="news_content" id="news_content"></textarea>
        </div>
        <div class="ClearFix"></div>
        <div class="Row">
            <!--<label>Enter the page URL to see the full details.</label>-->
            <input type="text" name="news_websitePath" id="news_websitePath" value="<?php date_default_timezone_set('GMT');echo "/news/".date("d")."/".date("m")."/".date("Y")."/";?>">
        </div>
        <div class="ClearFix"></div>
        <div class="Row">
            <input name="submitted" type="submit" />
        </div>
    </div>
</form>

所做的修改

网站路径现已本地化。

关于绝对路径和相对路径

如评论中所述,您应该删除路径的Desktop部分,这就是为什么

由于您的路径不以/开头:

"Desktop/IMG/BananzaNews/Thumbs/" . $_FILES["file"]["name"]);

PHP将评估它相对于包含已执行PHP脚本的文件夹这是您在注释中提到的文件夹Desktop

http://rafflebananza.com/Desktop/index.php

这意味着PHP实际上会认为你想将你的图像保存到这个地方:

"[root]/Desktop/Desktop/IMG/BananzaNews/Thumbs/[...]"

我认为情况并非如此。

您可以删除桌面,也可以使用桌面的绝对路径。要找到它,您可以使用getcwd();

然后,您的路径将如下所示:

/Applications/MAMP/htdocs/Desktop/...
/home/rafflebananza/Desktop/...

记得:

  • 绝对路径must
  • 前面加上/
  • 相对路径must not
  • 前面加上/
  • ./引用当前文件夹
  • ../是指父文件夹
  • 您可以将它们堆叠./../folderInParentFolder/file.php