PHP文件上传器无法正常工作

时间:2014-03-18 20:11:40

标签: php

我尝试过所有想法。当我想在我的服务器上上传照片时,名称写入db,但没有上传到服务器。

HTML

<form role="form" action="db_work.php?index=list_slider&action=new" method="post" ENCTYPE="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="512000"/>

PHP

   $targetFolder = 'uploads/foto/slider'; // Relative to the root
            $tempFile = $_FILES['Filedata']['tmp_name'];
            $targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
            $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

            // Validate the file type
            $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
            $fileParts = pathinfo($_FILES['Filedata']['name']);
            $fileName = $_FILES['Filedata']['name'];
            $fileName = strtolower ($fileName);

            $mysqli->query("UPDATE list_slider SET item_file = '$fileName' WHERE id='$id'");

            if (in_array($fileParts['extension'],$fileTypes)) {
                move_uploaded_file($tempFile,$targetFile);
                echo '1';
            } else {
                echo 'Invalid file type.';
            }

此脚本位于文件夹admin中。在根目录中是文件夹上传。

1 个答案:

答案 0 :(得分:1)

<!doctype html>
<html lan="en">
<head>
<title>Upload Script</title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<table align="center" width="60%">
<tr>
<td colspan="3" align="center">
<h2>Upload Image</h2>
</td>
</tr>
<input type="hidden" name="insert" value="updetails" />
<tr>
<td><label for="photo">Upload Image</label></td>
<td width="5%">:</td>
<td><input name="uploadimage" type="file" id="photo" /></td>
</tr>
<tr height="20"> </tr>
<tr>
<td colspan="3" align="center">
<input type="reset" value="Reset" />
<input type="submit" value="Upload" name="submit"/>
</td>
</tr>
</table>
</form>
</body>
</html>

uploader.php

if($_POST['submit']=='Upload')
{
    $allowedExts = array("gif", "jpeg", "jpg", "png");

    $info = pathinfo($_FILES['uploadimage']['name']);
    $ext = $info['extension']; // get the extension of the file

    $target_path = "uploads/";
    $target_path = $target_path.str_replace(' ', '_', $_FILES["uploadimage"]["name"]);

    if ((($_FILES["uploadimage"]["type"] == "image/gif") || ($_FILES["uploadimage"]["type"] == "image/jpeg")
        || ($_FILES["uploadimage"]["type"] == "image/jpg") || ($_FILES["uploadimage"]["type"] == "image/pjpeg")
        || ($_FILES["uploadimage"]["type"] == "image/x-png")|| ($_FILES["uploadimage"]["type"] == "image/png"))
        && ($_FILES["uploadimage"]["size"] < 5242880) && in_array($ext, $allowedExts))  //5242880 Maximum File size in bytes (5mb)
    {
        if ($_FILES["uploadimage"]["error"] > 0) {
            echo "Return Code: " . $_FILES["uploadimage"]["error"] . "<br>";
        }
        else {
            move_uploaded_file($_FILES['uploadimage']['tmp_name'], $target_path);

            echo "<br />The image ".  basename( $_FILES['uploadimage']['name'])." has been uploaded";

        }
    }
    else
    {
        echo "Invalid file";
    }
}
else
{
    echo"<br /><b>You Dont have permission to open this Page</b>";
}