如何将我上传到我的服务器链接的图片链接到我的网站

时间:2015-09-03 05:01:06

标签: php file-upload

我创建了一个基本的汽车销售网站。我使用以下PHP代码上传我的图像

$target_folder = "Cars_Photos/";
$target_path = $target_folder . basename( $_FILES['fileToUpload']['name'] );

//echo $target_path . '<br><br><br>';
//print_r($_FILES);


print($_FILES['fileToUpload']['tmp_name']);

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

所以文件正在上传到服务器,这很棒(花了我很多时间让它工作),我使用phpliteadmin作为我的数据库管理器,我有一个名为car_image_url的字段,这是我将网址粘贴到服务器上的图片的位置,但是我在网站上添加了一个用户可以自己上传图片的页面,所以我的问题是如何才能让它工作。

我使用以下内容转换网址以显示图片。

echo "<td id='img'><img src=\"".  $row["car_image_url"] . "\" /></td>";

然而,上传文件是一个不同的故事,我在我的网站上使用什么代码来上传文件以链接到图片网址。

这是我在主站点上制作新车的PHP代码:

<?php

try {
    # Connect to SQLite database
    $dbh = new PDO("sqlite:../Car_Sales_Network");

    $make = $_POST['Make'];
    $model = $_POST['Model'];
    $badge = $_POST['Badge'];
    $price = $_POST['Price'];
    $trans = $_POST['Transmission'];
    $ppl = $_POST['P_Plate_Legal'];

    $sth = $dbh->prepare('INSERT INTO Cars_On_Network
                         ("car_make","car_model","car_badge","price","trans","P_Plate_Legal")
                         VALUES
                         (?, ?, ?, ?, ?, ?)');

    $sth->execute(array($make, $model, $badge, $price, $trans, $ppl));

    $id = $dbh->lastInsertId();

    //header("Location: ../Carsales_Network.php");
}

catch(PDOException $e) {
    echo $e->getMessage();
}

$target_folder = "Cars_Photos/";
$target_path = $target_folder . basename( $_FILES['fileToUpload']['name'] );

//echo $target_path . '<br><br><br>';
//print_r($_FILES);


print($_FILES['fileToUpload']['tmp_name']);

if(move_uploaded_file($_FILES['fileToUpload']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}
?>

这是HTML:

<!DOCTYPE html>

<html>
<head>
    <title>New Vehicle</title>
    <link type="text/css" rel="stylesheet" href="New_Car_Form.css"/>
</head>

<body>
        <div id="main">
        <form action="Insert_Car.php" method="post" enctype="multipart/form-data">
        Make:<br>
        <input type="text" name="Make">
        <br>
        Model:<br>
        <input type="text" name="Model">
        <br><br>
        Badge:<br>
        <input type="text" name="Badge">
        <br>
        Price:<br>
        <input type="text" name="Price">
        <br>


        Transmission: <br>
        <input type="radio" name="Transmission" value="Manual" checked>Manual
        <br>
        <input type="radio" name="Transmission" value="Auto">Automatic
        <br><br>

        P Plate Legal: <br>
        <select name="P_Plate_Legal">
          <option value="Yes">Yes</option>
          <option value="No">No</option>
        </select>
        <br>


        Choose a Picture: <br>
        <input type="file" name="fileToUpload" id="fileToUpload">
        <input type="submit" value="Upload Image" name="submit">
        </form>
        <br>
        <br>
        <input class="submit" type="submit" value="Submit">
        <br>
        <a href="http://1673-itstudies/12-infotech/jsummers/Carsales_Network.php" class="myButton">Let's go back!</a>
        <br>
    </div>
</body>
</html>
</form>

</body>
</html>

我确信这是一个类似的过程,但我无法想到如何做到这一点。

干杯。

1 个答案:

答案 0 :(得分:0)

在我看来,您上传脚本时遇到了问题。

这是一种派上用场的方式(它为所有图像创建一个唯一的名称)

在我写的WRITE TO DATABASE评论中包装你的sql:

 if(isset($_FILES['fileToUpload'])){

$File = $_FILES['fileToUpload'];


//File properties:
$FileName = $File['name'];
$TmpLocation = $File['tmp_name'];
$FileSize = $File['size'];
$FileError = $File['error'];


//Figure out what kind of file this is:
$FileExt = explode('.', $FileName);

$FileExt = strtolower(end($FileExt));


//Allowed files:
$Allowed = array('jpg', 'png', 'jpeg', 'gif');


//Check if file is allowed:
if(in_array($FileExt, $Allowed)){

    //Does it return an error?
    if($FileError==0){


        //Create new filename:
        $NewName = uniqid('', true) . '.' . $FileExt;

        //Destination
        $UploadDestination = $_SERVER['DOCUMENT_ROOT'] . '/Cars_Photos/' . $NewName;

        //Move file to location:
        if(move_uploaded_file($TmpLocation, $UploadDestination)){

            //Filename = $NewName


            //WRITE TO DATABASE:


            //encode url:
            $Image = urlencode($UploadDestination);

            //Redirect:
            header("Location: /complete.php?image=$Image&cat=Cars");
        }

        //File didnt upload:
        else{
            echo "File didnt upload...";
        }
    }

    //An error occured
    else{
        echo "An error occured while uploading...";
    }

}
//Filetype not allowed
else{
    echo "Sorry, the file you tried to upload is not allowed.";
}



}

Complete.php:

<?php
 //Your file is here:
 echo urldecode($_GET['image']);