检查上传文件中的路径并显示

时间:2013-10-31 13:44:22

标签: php

我有2个显示上传图片的php文件。 首先是form_upload.php

<html>
<head><title>File Upload</title></head>
<body>
<ol>
    <li>Enter the file name of the product picture you want to upload or the the browse button to navigate to the picture file</li>
    <li>when the path to the picture file shpws in the text field, click the upload picture</li>
</ol>

<form enctype="multipart/form-data" action="uploadFile.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="500000"/>
<input type="file" name="pix" size="60"/>
<p><input type='submit' name="Upload" value="Upload picture"/></p>
</form>
</body>
</html>

第二个是uploadedFile.php

<?php

if(!isset($_POST['Upload']))
{
    include("form_upload.php");
}
else
{
    if($_FILES['pix']['tmp_name']=="") //check wether the file is larger than 2mb or not
    {
        echo "file did not successfully upload. Check the file size. File must be less than 500K";
        include("form_upload.php");
        exit();
    }
    if(!preg_match("/image\/jpeg/",$_FILES['pix']['type']))
    {
        echo "only jpg files are allowed. Please try another file";
        include("form_upload.php");
        exit();
    }
    else
    {
        $destination='C:\xampp\htdocs\test\hinh\ '.$_FILES['pix']['name'];
        $temp_file=$_FILES['pix']['tmp_name'];
            move_uploaded_file($temp_file,$destination);
        echo "<p>the file has successfully uploaded :{$_FILES['pix']['name']} {$_FILES['pix']['type']} ({$_FILES['pix']['size']}) </p>";
        echo "<img src='C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}' alt='picture'><br/>";
        echo "C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}";

    }
}
?>

上传后我无法显示图片。我检查了路径是否正确。但是,当我把 它在标签中不显示图片这里是我的语法:

echo "<img src='C:\\xampp\\htdocs\\test\hinh\\{$_FILES['pix']['name']}' alt='picture'><br/>";

1 个答案:

答案 0 :(得分:0)

您不应该在驱动器“C”上使用文件系统路径引用该文件。您应该使用HTTP链接,最好是相对链接,如下所示:

echo "<img src='/path/to/upload/{$_FILES['pix']['name']}' alt='picture'><br/>";

我不知道您的网络服务器上如何命名该路径。你必须找出自己正确的事情。我想这可能是/test/hinh/