2015-08-21 30 views
-1

我正在这里工作。在网站的一部分上,管理员可以上传图片,并将其存储在服务器上,并根据位置和上传时间输入数据库。PHP不能正常工作本地主机

我在本地工作,但使用WAMP作为测试服务器。如果我手动运行localhost/includes/db.php(数据库连接)在地址栏中,我可以看到它连接,因为我得到“连接成功”。

<?php 

try { 

    $dbname = "woody_tools"; 
    $user = "site"; 
    $pass = "site"; 
    $host = "localhost"; 


    # MySQL with PDO_MYSQL 
    $conn = new PDO("mysql:host=$host;dbname=$dbname", $user, $pass); 
    // set the PDO error mode to exception 
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
    echo "Connected successfully"; 
    } 

catch(PDOException $e) 
    { 
    echo "Connection failed: " . $e->getMessage(); 
    } 

?> 

这是我使用管理员选择并上传图片的页面的窗体控件。

<form action="includes/saveimage.php" enctype="multipart/form-data" method="post"> 

      <table style="border-collapse: collapse; font: 12px Tahoma;" border="1" cellspacing="5" cellpadding="5"> <!-- Table to upload products --> 
      <tbody> 
      <tr> 
       <td><input name="uploadedimage" type="file"></td> 
      </tr> 
      <tr> 
       <td><input name="Upload Now" type="submit" value="Upload Image"></td> 
      </tr> 
      </tbody> 
      </table> <!-- Table to upload products --> 
     </form> 
     </div> 
    </div> 

而应该上传图片和更新数据库的PHP页面的代码。

<?php 
include("includes/db.php"); 

    function GetImageExtension($imagetype) 
    { 
     if(empty($imagetype)) return false; 
     switch($imagetype) 
     { 
      case 'image/bmp': return '.bmp'; 
      case 'image/gif': return '.gif'; 
      case 'image/jpeg': return '.jpg'; 
      case 'image/png': return '.png'; 
      default: return false; 
     } 
    } 



if (!empty($_FILES["uploadedimage"]["name"])) { 

    $file_name=$_FILES["uploadedimage"]["name"]; 
    $temp_name=$_FILES["uploadedimage"]["tmp_name"]; 
    $imgtype=$_FILES["uploadedimage"]["type"]; 
    $ext= GetImageExtension($imgtype); 
    $imagename=$_FILES["uploadedimage"]["name"]; 
    $target_path = "Product_Images/".$imagename; 


if(move_uploaded_file($temp_name, $target_path)) { 

    $query_upload="INSERT into 'images_tbl' ('images_path','submission_date') VALUES 

('".$target_path."','".date("Y-m-d")."')"; 
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 

}else{ 

    exit("Error While uploading image on the server"); 
} 

} 

?> 

当我运行此代码时,我可以选择一张图片并点击“上传”,但这是出错的地方。我收到以下错误。请指向正确的方向。

Snippet of the error message

我知道数据库连接使用PDO和saveimages.php没有。我从教程中找到了这个脚本here。一旦我得到它的工作,我会尝试将其翻译成PDO。由于我正在做这一切,以教我自己,我试图采取婴儿的步骤。此外,我确认在运行这些数据后没有数据添加到数据库中。

谢谢!的崇高

+0

你编辑了你的php.ini吗?你在哪个端口上运行这个本地主机会话? –

+0

确保文件存在。 PHP说他们没有。 –

+0

我没有编辑它没有。它运行在默认设置之上。现在去检查。 –

回答

0

目录结构片段:

enter image description here

+0

有没有必要回答完成你的问题,编辑它并插入新信息 –

+0

我想到了,因为我提交了它...对不起。 –

0

你必须调用include("db.php");而不是include("includes/db.php");因为saveimage.php位于includes目录。

您还必须确保Product_Images目录位于includes目录中,并且您的网络服务器有权写入该目录。

+0

我已经完成了,现在我仍然收到这个错误。解析错误:语法错误,意外'函数'(T_FUNCTION)在C:\ wamp \ www \ woody \ includes \ saveimage.php在第4行 我假设PHP不喜欢“GetImageExtension($ imagetype)”..我做了一些搜索和老版本5.3及以下版本有我正在运行的这个问题5.5.12 –

+0

请告诉我你的'saveimage.php'的前5行,我认为有一些类似的东西丢失。 –

0

谢谢大家。 @Aedix Rhinedale有正确的答案。我忘了。他还给了我一些很好的选择!

相关问题