upload file with php and save path to sql - php

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.

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';
}

get the name of image when i upload it

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 '.'

Multiple Upload using CTRL Key PHP

would you help for my code , i need to do the multiple upload but i cant so will you help me please. i need so bad.
here's my code
i made multiple upload the form but it is not working. the output is "error array"
//HTML
<html>
<head>
<form name="Image" enctype="multipart/form-data" action="upload.php" method="POST">
<h1><font face="tahoma"> UPLOAD FILES</h1>
<label for="file">Filename:</label>
<input type="file" name="Photo[]" accept="image/*" multiple="multiple"/><br/><br/>
<input type="hidden" id="pageName" name="pageName">
<script type="text/javascript">
//get page name from parent
var value = window.opener.pageName
document.getElementById("pageName").value = value;
</script>
<INPUT type="submit" class="button" name="Submit" value=" Upload ">
<INPUT type="reset" class="button" value="Cancel"><br/><br/>
</form>
</head>
</html>
//PHP this is were upload is do.
<?php
include('global.php');
?>
<?
$uploadDir = 'directory/'; //Image Upload Folder
if(isset($_POST['Submit']))
{
$fileName = $_FILES['Photo']['name'][0];
$fileName1 = $_FILES['Photo']['name'][1];
$tmpName = $_FILES['Photo']['tmp_name'];
$fileSize = $_FILES['Photo']['size'];
$fileType = $_FILES['Photo']['type'];
$filePath = $uploadDir . $fileName . $fileName1;
//upload error
if ($_FILES["Photo"]["error"] > 0)
{
echo "Error: " . $_FILES["Photo"]["error"] . "<br />";
}
//photo already exixts
else
//insert image into DB
{
move_uploaded_file($tmpName, $filePath);
$filePath = addslashes($filePath);
$filePath = stripslashes($filePath);
$filePath = mysql_real_escape_string($filePath);
$query = "INSERT INTO images (image , category ) VALUES ('$filePath', '$pageName')";
mysql_query($query) or die('Error, query failed');
echo" Upload Successful. <br/> <br/>";
echo "Stored in: " . "directory/" . $_FILES["Photo"]["name"];
?>
<br/><br/>
<img width="300" height="400" src="directory /<?=$_FILES["Photo"]["name"]?>"><br/>
<?
}
}
?>
Error: Array is telling you that the error being returned is actually an Array object, not a string. If you want to see the actual error message, you need to view the full contents of the array. Something like print_r($_FILES["Photo"]["error"]); or looping through the array like so
foreach($_FILES["Photo"]["error"] as $err) {
echo "error: " . $err . "<br>";
}
Or you can just print the first error returned just as you have returned the first file in your name array echo $_FILES["Photo"]["error"][0];

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.

Categories