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
This question is related to
php
file-upload
<html>
<head>
<title>PHP Reanme image example</title>
</head>
<body>
<form action="fileupload.php" enctype="multipart/form-data" method="post">
Select image :
<input type="file" name="file"><br/>
Enter image name :<input type="text" name="filename"><br/>
<input type="submit" value="Upload" name="Submit1">
</form>
<?php
if(isset($_POST['Submit1']))
{
$extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
$name = $_POST["filename"];
move_uploaded_file($_FILES["file"]["tmp_name"], $name.".".$extension);
echo "Old Image Name = ". $_FILES["file"]["name"]."<br/>";
echo "New Image Name = " . $name.".".$extension;
}
?>
</body>
</html>
Click [here] (https://meeraacademy.com/php-rename-image-while-image-uploading/
Just get the file extention then assign the file a new name with uniqid and pass the new name to the move_upload_file method. For example:
if(isset($_POST['submit'])){
$total = count($_FILES['files']['tmp_name']);
for($i=0;$i<$total;$i++){
$fileName = $_FILES['files']['name'][$i];
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
$newFileName = uniqid();
$fileDest = 'filesUploaded/'.$newFileName.'.'.$ext;
if($ext === 'pdf' || 'jpeg' || 'JPG'){
move_uploaded_file($_FILES['files']['tmp_name'][$i], $fileDest);
$fileUpload = $newFileName.'.'.$ext[$i].',<br>';
}else{
echo 'Pdfs and jpegs only please';
}
}
}
Here is the code in PHP
to upload an image, save it to the database, display it and save it to a folder.
At first, HTML
code for the 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>
The 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);
}
?>
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;
}
}
?>
i know what went wrong you see you used "" instead of '' in this
$target_Path = $target_Path.basename(
"myFile.png"
);
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);
}
}
?>
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);
?>
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 :)
Source: Stackoverflow.com