get the name of image when i upload it - php

When I upload image :
<h3 id="txt_i2">Img</h3><input type="file" accept="image/jpeg, image/jpg" name="picture" size="chars">
How to get the name of the uploaded img using php ? I don't mean "picture". For example if the img is called cow.jpg the get that "cow"

Anton, you can use the code below to get the image or any file name without extension.
$filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);

POST method uploads :
$_FILES['userfile']['name']
The original name of the file on the client machine.
So using the above, if you want the actual file name cow.jpg, it is stored in
$_FILES['picture']['name'];
If you want the name of the file without the extension, you can extrapolate that from the filename provided with pathinfo() by setting the PATHINFO_FILENAME flag, which would return cow. If the image is named cow.moo.jpg it will return cow.moo:
$picture_filename = pathinfo($_FILES['picture']['name'], PATHINFO_FILENAME);

Use this
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
Uploade Script
<?php
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
?>
This give you file name, file type and file size.

check pathinfo there are some examples, so you just need to use pathinfo on $_FILES variable

In html, you should add a property to form element: enctype="multipart/form-data".

$filename = explode(".", $_FILES['picture']['name']); //split by '.'
array_pop($filename); //remove the last segment
$filename = implode(".", $filename); //concat it by '.'

Related

Send file to server derictory over form with user's name

I have a form.html
<form action="user.php" method="post" enctype="multipart/form-data" >
<input type="text" size="40" name="UserCallFile" >
<input type="file" name="filename"><br>
<input type="submit" value="Send"><br>
</form>
And user.php
<?php
if(is_uploaded_file($_FILES["filename"]["tmp_name"]))
{
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_FILES["filename"]["name"]);
} else {
echo("Error");
}
?>
I need that user could download file to server which name of this file he written on text input. Ho to do that?
I tried this way
move_uploaded_file($_FILES["filename"]["tmp_name"], "img/" . $_POST["fileName"] . "." . "png");
But of course here "png" have to be logic with any file extension.
I hope you are understand the idea of problem.
Simply try to play with this..
$image = $_FILES["filename"]["tmp_name"];
$dir = "gallery/"; //adjust this correctly, watch for slashes depending on your website config
$newname = $image['name'];
if(!#copy($image, $dir . $newname))
{
echo 'error';
}

File Upload not Working in PHP

I am new to PHP and am trying to create a page to upload a jpeg file. The webpage seems to run fine and it appears the file is uploading, however the file is not appearing on the server. Any help you can provide will be great.
The PHP code is:
<?php
$target_dir="/var/www/html/";
$fileName=$_FILES['file']['name'];
$target_file=$target_dir . basename($fileName);
$imageFileType=pathinfo($target_file,PATHINFO_EXTENSION);
$fileTempName=$_FILES["file"]["tmp_name"];
$fileType=$_FILES["file"]["type"];
$fileSize=$_FILES["file"]["size"];
$fileError=$_FILES["file"]["error"];
if(($fileType=="image/jpeg")&&($fileSize<100000)){
if($fileError>0){
echo "Return Code: " . $fileError . "<br />";
}
else{
echo "Upload: " .$fileName . "<br />";
echo "Type: " . $fileType . "<br />";
echo "Size: " . ($fileSize / 1024) . " kb<br />";
echo "Temp file: " . $fileTempName . "<br />";
if (file_exists($fileName)){
unlink($fileName);
}
move_uploaded_file($fileTempName,$target_file);
echo "<br><br>File Temp Name: " .$fileTempName."\r\n <br>";
echo "Uploaded file stored as : " .$target_file ."<br><br>";
}
}
else{
echo "File is not a JPEG or too big.";
}
?>
And the HTML code is as follows:
<html>
<body>
<form action="save2web.php" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"/>
<br/>
<input type="submit" name="submit" value="Upload"/>
</form>
</body>
</html>
The problem is that, you don't check the returned value of move_uploaded_file() at all. But looks like, you're running your script on the host, and since most host disallow relative paths, you'd better provide an absolute.
So try, replacing:
$target_dir = "/var/www/html/"; // <- This is relative, which might be blocked due to security reasons
with
$target_dir = dirname(__FILE__) . "/var/www/html/"; // dirname(__FILE__) is a path to root
And then, make sure that the file was uploaded:
if (!move_uploaded_file(...)) {
// error
}
Presuming your server is linux from the path, you may need to change the permissions of the folder your trying to upload to, try changing the folder owner to www-data, this fixed the same issue for me.
Change your target directory like this. Add a .(dot) before the directory name.
$target_directory = './var/www/html';

How do you save an image from webpage to server?

I have an image id='canvasImg' and I can not figure out how to take this image and save it to the server.
I tried saving the contents to a form and then putting the contents in a png file after decode but it didn't work.
You will need to use a simple file form upload
HTML
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
PHP (upload_file.php)
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
} else {
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}

Upload image file using href in move_uploaded_file( ) php

I need some advice and guide line.
I am trying to upload image into a folder using link. And I am unable to upload an image.
I am trying this code. This shows error message :
[function.move-uploaded-file]: failed to open stream: HTTP wrapper does not support writeable connections
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
<html>
<body>
<form action="" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Is it possible ?
The destination of the moved file needs to be an absolute path to the server, i.e.
move_uploaded_file( $_FILES["file"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] ."/uploads/" . $_FILES["file"]["name"]);
You are supplying file_exists and move_uploaded_file with an URL. Try supplying a file on the hard drive instead. Change:
file_exists("http://localhost.myimage.com/uploads/" . $_FILES["file"]["name"])
to
file_exists($_SERVER['DOCUMENT_ROOT'] . "uploads/" . $_FILES["file"]["name"])
and similar to move_uploaded_file since that's where the error originated.
No it's not possible this way.
You try to store the uploaded file on a wrong destination.
I assume you own "http://localhost.myimage.com/uploads/" and wan't to store the file under '/uploads/' in your server.
If your project is installed e.g. under /var/www/my_prj/
And your upload is under /var/www/my_prj/uploads
then your code should look like:
move_uploaded_file($_FILES["file"]["tmp_name"],
"/uploads/" . $_FILES["file"]["name"]);
This only applies when '/var/www/my_prj/' is the DocumentRoot for your apache configured host.

upload file with php and save path to sql

Does anyone know any good tutorial on how to upload a file with php and save the files path to a sql server?
To upload a file you need at least a HTML POST form with multipart/form-data encoding. Therein you put an input type="file" field to browse the file and a submit button to submit the form.
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
In the upload.php the uploaded file is accesible by $_FILES with the field name as key.
$file = $_FILES['file'];
You can get its name as follows:
$name = $file['name'];
You need to move it to a permanent location using move_uploaded_file(), else it will get lost:
$path = "/uploads/" . basename($name);
if (move_uploaded_file($file['tmp_name'], $path)) {
// Move succeed.
} else {
// Move failed. Possible duplicate?
}
You can store the path in database the usual way:
$sql = "INSERT INTO file (path) VALUES ('" . mysqli_real_escape_string($path) . "')";
// ...
From http://www.w3schools.com/php/php_file_upload.asp
HTML
<html>
<body>
<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"]; //<- This is it
}
}
?>
Note that to upload the file you need to specify the path to save the file. If you save the file you already know it path.

Categories