php-如何使用所需的nam上传和保存文件

我正在使用此代码上传文件(图像到文件夹)

<form action='' method='POST' enctype='multipart/form-data'>
<input type='file' name='userFile'><br>
<input type='submit' name='upload_btn' value='upload'>
</form>

<?php
$target_Path = "images/";
$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target_Path );
?>

当文件(图像)保存在指定路径时...如果我想用某个想要的名称保存文件...

我曾尝试更换此

$target_Path = $target_Path.basename( $_FILES['userFile']['name'] );

有了这个

$target_Path = $target_Path.basename( "myFile.png" );

但是它不起作用

Moon asked 2020-02-16T19:28:40Z
7个解决方案
106 votes

你可以试试看

$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".$ext; 

$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target);
Manie answered 2020-02-16T19:28:47Z
7 votes

这会很好-您可以使用HTML5仅允许上传图片文件。这是uploader.htm的代码-

<html>    
    <head>
        <script>
            function validateForm(){
                var image = document.getElementById("image").value;
                var name = document.getElementById("name").value;
                if (image =='')
                {
                    return false;
                }
                if(name =='')
                {
                    return false;
                } 
                else 
                {
                    return true;
                } 
                return false;
            }
        </script>
    </head>

    <body>
        <form method="post" action="upload.php" enctype="multipart/form-data">
            <input type="text" name="ext" size="30"/>
            <input type="text" name="name" id="name" size="30"/>
            <input type="file" accept="image/*" name="image" id="image" />
            <input type="submit" value='Save' onclick="return validateForm()"/>
        </form>
    </body>
</html>

现在是upload.php的代码-

<?php  
$name = $_POST['name'];
$ext = $_POST['ext'];
if (isset($_FILES['image']['name']))
{
    $saveto = "$name.$ext";
    move_uploaded_file($_FILES['image']['tmp_name'], $saveto);
    $typeok = TRUE;
    switch($_FILES['image']['type'])
    {
        case "image/gif": $src = imagecreatefromgif($saveto); break;
        case "image/jpeg": // Both regular and progressive jpegs
        case "image/pjpeg": $src = imagecreatefromjpeg($saveto); break;
        case "image/png": $src = imagecreatefrompng($saveto); break;
        default: $typeok = FALSE; break;
    }
    if ($typeok)
    {
        list($w, $h) = getimagesize($saveto);
        $max = 100;
        $tw = $w;
        $th = $h;
        if ($w > $h && $max < $w)
        {
            $th = $max / $w * $h;
            $tw = $max;
        }
        elseif ($h > $w && $max < $h)
        {
            $tw = $max / $h * $w;
            $th = $max;
        }
        elseif ($max < $w)
        {
            $tw = $th = $max;
        }

        $tmp = imagecreatetruecolor($tw, $th);      
        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
        imageconvolution($tmp, array( // Sharpen image
            array(−1, −1, −1),
            array(−1, 16, −1),
            array(−1, −1, −1)      
        ), 8, 0);
        imagejpeg($tmp, $saveto);
        imagedestroy($tmp);
        imagedestroy($src);
    }
}
?>
NULL-POINTER answered 2020-02-16T19:29:12Z
1 votes

您可以从此处获取演示源代码:[http://abhinavsingh.com/blog/2008/05/gmail-type-attachment-how-to-make-one/]

它可以随时使用,也可以进行修改以适合您的应用程序需求。希望能帮助到你 :)

Abhinav Singh answered 2020-02-16T19:29:37Z
0 votes

使用它作为上传的目标路径

<?php
$file_name = $_FILES["csvFile"]["name"];
$target_path = $dir = plugin_dir_path( __FILE__ )."\\upload\\". $file_name;
echo $target_path;
move_uploaded_file($_FILES["csvFile"]["tmp_name"],$target_path. $file_name);
?>
harish kumar answered 2020-02-16T19:29:57Z
0 votes

配置“ php.ini”文件

首先,确保将PHP配置为允许文件上传。在您的“ php.ini”文件中,搜索file_uploads指令,并将其设置为On:

file_uploads = On 

创建HTML表单

接下来,创建一个HTML表单,该表单允许用户选择他们要上传的图像文件:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
    Select image to upload:
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

</body>
</html>

上面的HTML表单应遵循的一些规则:     确保表单使用method =“ post”     该表单还需要以下属性:enctype =“ multipart / form-data”。 它指定提交表单时要使用的内容类型如果没有上述要求,文件上传将无法进行。其他注意事项:     标记的type =“ file”属性将输入字段显示为文件选择控件,在输入控件旁边带有“浏览”按钮上面的表格将数据发送到名为“ upload.php”的文件,我们将在接下来创建该文件。

创建上传文件PHP脚本

“ upload.php”文件包含用于上传文件的代码:

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
    if($check !== false) {
        echo "File is an image - " . $check["mime"] . ".";
        $uploadOk = 1;
    } else {
        echo "File is not an image.";
        $uploadOk = 0;
    }
}
?>
Mubin answered 2020-02-16T19:30:44Z
0 votes

我知道出了什么问题,您看到您在此使用“”而不是“

$target_Path = $target_Path.basename( 

   "myFile.png"

);
SuperMarioMC98 answered 2020-02-16T19:31:04Z
-2 votes

这是id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)中的代码,用于上载图像,将其保存到数据库,显示它并将其保存到文件夹。

  1. 首先,该表单的id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)代码为:

    id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)
  2. id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)代码

根据需要创建数据库和表。(仅需要2个字段)在表格中id(INT) 255 primary key AUTO INCREMENT and your image row(anyname) (MEDIUMBLOB)

<?php

if(isset($_POST['submit'])){
    if(@getimagesize($_FILES['image']['tmp_name']) == FALSE){
        echo "<span class='image_select'>please select an image</span>";

    }
    else{
        $image = addslashes($_FILES['image']['tmp_name']);
        $name  = addslashes($_FILES['image']['name']);
        $image = file_get_contents($image);
        $image = base64_encode($image);
        saveimage($name,$image);
        $uploaddir = 'profile/'; //this is your local directory
        $uploadfile = $uploaddir . basename($_FILES['image']['name']);

        echo "<p>";

            if (move_uploaded_file($_FILES['image']['tmp_name'], $uploadfile)) {// file uploaded and moved} 
            else { //uploaded but not moved}

        echo "</p>";



    }

}

displayimage();
function saveimage($name,$image)
{
    $con = mysql_connect("localhost","root","your database password");
    mysql_select_db("your database",$con);
    $qry  = "UPDATE your_table SET your_row_name='$image'";
        $result = @mysql_query($qry,$con);

    if($result)
    {
        echo "<span class='uploaded'>IMAGE UPLOADED</span>";

    }
    else
    {
        echo "<span class='upload_failed'>IMAGE NOT UPLOADED</span>";

    }
}
function displayimage()
{
    $con = mysql_connect("localhost","root","your_password");
    mysql_select_db("your_database",$con);
    $qry  = "select * from your_table";
    $result = mysql_query($qry,$con);

    while($row  = mysql_fetch_array($result))
    {
        echo '<img class="image" src="data:image;base64,'.$row[1].'">';

    }



    mysql_close($con);
}
?>
KRISHNENDU BISWAS answered 2020-02-16T19:31:42Z
translate from https://stackoverflow.com:/questions/3509333/how-to-upload-save-files-with-desired-name