当前位置: 动力学知识库 > 问答 > 编程问答 >

PHP files uploader not working

问题描述:

I have tried everythink. When I want upload foto on my server, name is write to db,but nothing uploaded to server.

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.';

}

this script is in folder admin. And in the root is folder uploads.

网友答案:
<!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>";
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: