PHP Image Upload, with preview on select of a file - php

Ok so im making an image uploader with a chance to see what you are going to upload, before entering it into the database. My mind is kinda tired, could you please just get me through this one? thx (btw it doesnt work :p) Its on a clean page, i choose a file from my pictures on compouter, it doesn echo "hi wazzup
<?php
if(isset($_FILES['image'])){
echo "hi wazzup!";
}
?>
<form name="form" id="form" method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" >
<input type="file" name="image" onChange="if(event.propertyName=='value') {document.getElementById("form").submit();}">
</form>

See what is the output and you will know wheater your javascript is wrong or you should use $_FILES instead of $_POST for image
<?php
if(isset($_POST))
{
if(isset($_FILES['image'])){
echo "<span style=\"z-index:1000;display:block;\">hi wazzup!</span>";
}
echo "POSTED";
}
?>

Related

Cant get the path of uploaded image in php

I am trying to retrieve the path of an image that I upload in the html page using php, but when i use the method $_FILES['file1']['tmp_name'] it displays an error saying "undifined index: file1".
This is the code I am trying:
HTML:
<form name="form1" method="GET" action="image.php">
<br/>
<input type="file" name="file1"/>
<input type="submit" value="submit"/>
</form>
PHP:
<?php
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
}
else
die("Not Uploaded");
?>
Uploading files require a POST method and not a GET.
Consult:
http://php.net/manual/en/features.file-upload.post-method.php
Also make sure the folder you are wanting to upload to, has proper permissions to write to it.
Sidenote edit: Plus, as stated in the other answer, a valid enctype is required.
I'd also use:
if ($_SERVER['REQUEST_METHOD'] == 'POST'){...}
In order to ensure of a POST method, while using full bracing for your else and adding another else{...} for the above.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
$files=$_FILES['file1']['tmp_name'];
$folder="images";
if(is_uploaded_file($_FILES['file1']['tmp_name']))
{
move_uploaded_file($_FILES['file1']['tmp_name'], $folder . '/'.$_FILES['file1']['name']);
$image=$folder . '/'.$_FILES['file1']['name'];
echo "Successfully Uploaded";
echo $image;
} else {
die("Not Uploaded");
}
} else{
echo "A POST method was not used here.";
}
Also a conditional !empty() against your file input.
http://php.net/manual/en/function.empty.php
Change:
<form name="form1" method="GET" action="image.php">
to
<form name="form1" method="POST" enctype="multipart/form-data" action="image.php">

how do I move my uploaded file to different folder

Hi Hi trying to upload a file via a form move it to another folder and then print its name. But doesnt work don't know why.
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/");
echo $_FILES['files']['name'];
}
?>
form missing:
enctype="multipart/form-data"
ref:
spec
<form method="post" action='exercice.php' id="form1">
<input type="file" name="files" id="files" onChange="submitForm();">
</form>
<?php
if (isset($_FILES['files']))
{
move_uploaded_file($_FILES['files']['tmp_name'], "uploaded/".$_FILES['files']['name']);
echo $_FILES['files']['name'];
}
?>
You need to include the file name in the upload path, like shown above.
Also, where is your upload (ajax) code?
Did isset($_FILES['files']) return true?

insert image into mssql using php

I want to insert image into mssql providing option for user to select the image and insert into database.so i used the following code
<html>
<head>
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
mmysql_connect("localhost","sample","welcome");
mysql_select_db("samples");
if (empty($_POST["frm"]))
{
}
else
{
$filename=trim($_REQUEST['slcfile']);
$datastring = file_get_contents($filename);
$data = unpack("H*hex", $datastring);
echo trim($_REQUEST['slcfile']);
mssql_query("insert into imageinserter values ( 0x".$data['hex'].",'.$filename.')");
}
}
?>
</head>
<body>
<form name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
select image :<input type="file" name="slcfile" /> <br>
<input type="submit" name="slcfile" value="addimage"/>
</form>
</body>
</html>
but it is saying error error at file_get_contents saying no such file or resource found.
Please help to solve the issue
Thanks in advance
use multipart/form-data in your form like this:
<form enctype="multipart/form-data" name="frm" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post">
and then read the uploaded file by its temp name
eg: $_FILES['uploadedfile']['tmp_name']
You need to allow
allow_url_fopen
in your php.ini config file. Some hosts disallow it for security

unable to make image upload optional in codeigniter

I have four images to upload. And all these four images are optional in my form.
I tried to make it optional but I could not solve my problem. Please check my code below and let me know any solution for it.
<?php
if(isset($_FILES))
{
if(empty($_FILES['testfile']['name']))
{
echo 'no';
}else{
// do the form validation here
}
}
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="POST" enctype="multipart/form-data">
<input type="file" name="testfile"/>
<input type="submit" value="upload">
</form>
here is some test you can try i hope it helps

<input type=file> not getting image

I am trying to get image file name by tag, But when I check it through isset($_FILES['imgFile']), it returns always false.
Here is my HTML tag for getting image file:
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
Here is my php code to retrieve it:
if(isset($_FILES['imgFile']))
{
$img = $_FILES['imgFile']['name'];
echo $img";
}
else
{
echo "Image not set";
}
It always generate "Image not set" as an output though I have selected an image.
Are you using the correct enctype on the form?
<form enctype="multipart/form-data">
This is required when using a file upload element.
just use:
enctype="multipart/form-data"
Say this is your form:
<form action="same_page.php" method="post" enctype="multipart/form-data">
<input type="file" name="imgFile" accept="image/*" id="imgFile" src=""/>
<input type="submit" name="upload" value="Upload" />
</form>
and your php code to retrive name of the image is in the same page where the form is, then your code should be like this:
<?php
if (isset($_POST["upload"])) {
if (isset($_FILES['imgFile'])) {
$img = $_FILES['imgFile']['name'];
echo $img;
} else {
echo "Image not set";
}
}
?>
But if your php code is in another page, then you only need to use enctype="multipart/form-data" in the form as mentioned in the other answers.

Categories