Home How to upload & Save Files with Desired name
Reply: 6

How to upload & Save Files with Desired name

Moon
1#
Moon Published in 2010-08-18 05:57:05Z

i am using this code to upload files(images to a folder)

<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 );
?>

when the file(image) is saved at the specified path... WHAT if i want to save the file with some desired name....

i have tried replacing THIS

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

WITH THIS

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

BUT it's not working

gsamaras
2#
gsamaras Reply to 2015-11-16 23:09:39Z

You can try this,

$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);
Community
3#
Community Reply to 2014-04-14 07:48:05Z

This would work very well -- You can use HTML5 to allow only image files to be uploaded. This is the code for 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>

Now the code for 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);
    }
}
?>
Abhinav Singh
4#
Abhinav Singh Reply to 2010-08-18 08:10:29Z

You can grab the demo source code from here: http://abhinavsingh.com/blog/2008/05/gmail-type-attachment-how-to-make-one/

It is ready to use, or you can modify to suit your application needs. Hope it helps :)

kleopatra
5#
kleopatra Reply to 2013-10-11 10:24:21Z

use this for target path for uploading

<?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);
?>
Mubin
6#
Mubin Reply to 2016-11-18 09:08:35Z

Configure The "php.ini" File

First, ensure that PHP is configured to allow file uploads. In your "php.ini" file, search for the file_uploads directive, and set it to On:

file_uploads = On 

Create The HTML Form

Next, create an HTML form that allow users to choose the image file they want to upload:

<!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>

Some rules to follow for the HTML form above: Make sure that the form uses method="post" The form also needs the following attribute: enctype="multipart/form-data". It specifies which content-type to use when submitting the form Without the requirements above, the file upload will not work. Other things to notice: The type="file" attribute of the tag shows the input field as a file-select control, with a "Browse" button next to the input control The form above sends data to a file called "upload.php", which we will create next.

Create The Upload File PHP Script

The "upload.php" file contains the code for uploading a file:

<?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;
    }
}
?>
manetsus
7#
manetsus Reply to 2015-07-29 16:25:50Z

HERE IS THE CODE IN PHP TO UPLOAD IMAGE INTO THE DATABASE,DISPLAY AND ALSO SAVE IT TO YOUR LOCAL FOLDER

  1. AT FIRST HTML CODE FOR FORM:

    <div class="upload">
        <form method="POST" enctype="multipart/form-data" id="imageform">
           <br>
           <input type="file" name="image" id="photoimg" >
           <br><br>
           <input type="submit" name="submit" value="UPLOAD">
       </form>
    </div>
    
  2. HERE IS THE WHOLE PHP CODE:

create database and table as you wish.(only required 2 fields) In the table, 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);
}
?>
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.386296 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO