【问题标题】:Uploading a file to the webserver with customization通过自定义将文件上传到网络服务器
【发布时间】:2013-05-28 03:37:56
【问题描述】:

好的,所以我希望能够通过 PHP 将图像上传到我的网络服务器,用于管理控制面板。 现在,我希望能够使用文件上传器上传图像,并使用文本框作为图像将被调用的文件名。干杯:D

【问题讨论】:

    标签: php html file web upload


    【解决方案1】:

    在 html 中使用标准格式

    <form enctype="multipart/form-data" action="uploader.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Filename: <input name="name" type="text" /> 
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" value="Upload File" />
    </form>
    

    然后是uploader.php文件:

    //get the data from the form
    $target_path = "uploads/";
    
    $name= $_POST['name'];
    
    $target_path = $target_path . $name;
    
    //this is the script which uploads the file
    if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded as" . $name;
    } else{
    echo "There was an error uploading the file, please try again!";
    }
    

    您可能想检查文件是否为图像.. 这样做:

    $max_size=20000; //in kb
    $allowedExts = array("gif", "jpeg", "jpg", "png");
    $extension = end(explode(".", $_FILES["file"]["name"]));
    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"] < $max_size)
    && in_array($extension, $allowedExts))
    {
    
    //here goes the script to upload the file!!
    
    }else{
    echo"The file is not supported";}
    

    【讨论】:

    猜你喜欢
    • 2017-04-05
    • 1970-01-01
    • 1970-01-01
    • 2016-01-19
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2011-01-09
    • 2011-02-05
    相关资源
    最近更新 更多