-1

堆栈溢出

我目前正在开发一个页面,我需要在其中制作一个新闻系统。

新闻系统一切正常,但需要一个功能来上传图片(在创建新闻时)并显示它们。

所以它应该像,上传图片(最大大小等),它将上传到我服务器上的一个文件夹,并在mysql数据库中创建路径。

最好的问候,克里斯蒂安

4

3 回答 3

1

形式

   <form action="accept-file.php" method="post" enctype="multipart/form-data">
        Your Photo: <input type="file" name="photo" size="25" />
        <input type="submit" name="submit" value="Submit" />
    </form>

php(接受文件.php)

//if they DID upload a file...
if($_FILES['photo']['name'])
{
    //if no errors...
    if(!$_FILES['photo']['error'])
    {
        //now is the time to modify the future file name and validate the file
        $new_file_name = strtolower($_FILES['photo']['tmp_name']); //rename file
        if($_FILES['photo']['size'] > (1024000)) //can't be larger than 1 MB
        {
            $valid_file = false;
            $message = 'Oops!  Your file\'s size is to large.';
        }

        //if the file has passed the test
        if($valid_file)
        {
            //move it to where we want it to be
            move_uploaded_file($_FILES['photo']['tmp_name'], 'uploads/'.$new_file_name);
            $message = 'Congratulations!  Your file was accepted.';
        }
    }
    //if there is an error...
    else
    {
        //set that to be the returned message
        $message = 'Ooops!  Your upload triggered the following error:  '.$_FILES['photo']['error'];
    }
}

//you get the following information for each file:
$_FILES['field_name']['name']
$_FILES['field_name']['size']
$_FILES['field_name']['type']
$_FILES['field_name']['tmp_name']
于 2013-11-08T10:02:39.567 回答
0

我现在已经设法创建了一个系统,我通过测试你所有的答案来解决这个问题!:) 还花了一些时间阅读 php.net 上的文档;)

最好的问候,克里斯蒂安

于 2013-11-11T10:17:57.933 回答
0

此代码供您参考:

<?php

  $allowedExts = array("gif", "jpeg", "jpg", "png");
  $temp = explode(".", $_FILES["file"]["name"]);
  $extension = end($temp);

  if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/pjpeg") || ($_FILES["file"]["type"] == "image/x-png") || ($_FILES["file"]["type"] == "image/png")) && ($_FILES["file"]["size"] < 20000) && in_array($extension, $allowedExts))  {

    if ($_FILES["file"]["error"] > 0) {
        echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

        if (file_exists("upload/" . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
        } else {
            if( move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]) ){
                echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
            }else{
                echo $_FILES["file"]["name"]." unable to store";
            }
        }
    }
  } else {
    echo "Invalid file";
  }
?>
于 2013-11-08T10:05:11.863 回答