I have coded a image upload script but when I click upload i get redirected to the upload page?
Here is the code:
$image1name = $_FILES['image1']['name'];
$image1crntloc = $_FILES['image1']['tmp_name'];
$image1ext = pathinfo($image1name, PATHINFO_EXTENSION);
$image1size = $_FILES['image1']['size'];
$allowedext = array("jpg","gif","png");
//check image 1 extension.
if (!in_array($image1ext,$allowedext))
{
echo "<script>alert(\"Image 1 has an invalid file.\");</script>";
}
else{
$image1final = md5(time($image1name));
$saveimage1 = "../images/".$image1final.".".$image1ext;
$image1uploadresult = move_uploaded_file($image1crntloc, $saveimage1);
if ($image1uploadresult == TRUE)
{
echo "uploaded.";
}
else{
echo "image not uploaded.";
}
as soon as i click upload, i get redirected to the page where the user selects the image ,i have also checked the directory and nothing gets uploaded?
There is no PHP error shown as well.
Any help?
Thanks.
CODE FOR FORM:
<form id="new-ad" name="new-ad" method="post" action="includes/create.php" enctype="multipart/form-data">
<div class="form-group animated fadeIn">
<label class="labelcustom" for "image1">Image #1:</label>
<br />
Select image to upload:
<input type="file" class="form-control" name="image1" id="image1" />
<input type="submit" name="submit" />
</form>
In your code you are missing the closing bracket on your else statement:
else {
echo "image not uploaded.";
}
Should be:
else {
echo "image not uploaded.";
}
}
In your form, you forgot the max file size. Add <input type="hidden" name="MAX_FILE_SIZE" value="2097152" /> to it, were value is the max size in bytes to accept in the form.
Related
I am trying to upload an image file to my server using some code I found on the internet but it doesn't work and I can't find out why.
<form method="post" action="?p=edit-profil&id=<?php echo($user_id); ?>">
<h2>Profil bearbeiten</h2>
<hr />
<h4>Profilbild ändern</h4>
<p style="margin-top: 5px;">(Zulässige Dateiformate: .png .jpg)</p>
<input type="hidden" name="size" value="1000000" style="padding: 6px;">
<div>
<input type="file" name="image" class="choose-image">
</div>
<div>
<input type="submit" name="add-personal-data" value="Speichern" class="submitbutton">
</div>
</form>
// Get image name
$image = $_FILES['image']['name'];
// image file directory
$target = "img/".basename($image);
$image_insert = 'img/'.$image;
$image_upload_statement = $pdo->prepare("UPDATE beschreibung SET profilbild = '$image_insert' WHERE user_id = '$user_id'");
$image_upload_statement->execute();
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
$msg = "Image uploaded successfully";
}else{
$msg = "Failed to upload image";
}
So usually image_insert should be someting like img/picture.png but I only get img/ and there is no upload aswell. Since I am quit new with working with this topic I have no more idea how to fix the problem.
Your form tag should have this attribute enctype="multipart/form-data" to be able to send the image to the php file.
I have simple PHP code for Upload Image On the server.
index.php
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" />
<button type="submit" name="btn-upload">upload</button>
</form>
upload.php
if(isset($_POST['btn-upload']))
{
$move = "/var/www/html/".$_FILES['file']['name'];
$sidd = $_FILES['file']['tmp_name'];
$siddName = basename($_FILES['file']['name']);
if (move_uploaded_file($sidd, $move)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Upload failed";
}
}
Any Idea:- How I can set image-background "white" and then upload on server ????? so image will show with white background on website and also store with white background.
Note:- I do not want to use any third party paid API.
Re asking how to check if $_POST[FILE] isset
I have a file input and if I submit my form without an image I want something to happen if I uploaded a file in the input I want something different to happen.
if (!isset($_POST[image])) { }
seems to trigger regardless of whether or not I have uploaded a file in the input or not.
<label>
<p>Profile Picture:</p>
<input type="file" name="image" value="" />
</label>
My last question was marked as a duplicate of this answer Check whether file is uploaded however
if (!file_exists($_FILE['image'])) { }
didn't work either it is still showing truthy even when an image is uploaded. So not the answer I need.
To check if there is a file uploaded is you need to check the size of the file.
Then to check if its an image or not is you need to use the getimagesize() function. See my script below:
HTML:
<form action="index.php?act=s" method="post" enctype="multipart/form-data">
<input type="file" name="image" value=""/>
<input type="submit">
</form>
PHP:
<?php
if(isset($_GET['act'])){
// Check if there is a file uploaded
if($_FILES["image"]["size"]>0){
echo "There is a file uploaded<br>";
// Check if its an image
$check_if_image = getimagesize($_FILES["image"]["tmp_name"]);
if($check_if_image !== false) {
echo "Image = " . $check_if_image["mime"] . ".";
} else {
echo "Not an image";
}
}
else{
echo "There is NO file uploaded<br>";
}
}
?>
I had file upload working fine this morning and did a couple of test uploads using small csv files, but when I did another test it has stopped working.
var_dump of $_FILES['file'] is NULL, and trying if($_FILES) produces nothing.
No one else uses my server so I know nothing's been changed.
Here is my form which posts to the same page.
<form method="post" enctype="multipart/form-data" style="border:1px solid #999">
<input type="file">
<input type="hidden" name="customerID" value="<?=$_GET['customerID']?>">
<input type="submit" value="Import">
</form>
Php handler:
if($_FILES)
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
$path = "files/".$_POST['customerID']."/";
echo $path."<br>";
if(!file_exists($path)) mkdir($path);
$path = $path.basename($_FILES['file']['name']);
echo $path."<br>".$_FILES['file']['tmp_name']."<br>";
if(is_uploaded_file($_FILES['file']['tmp_name']))
{
if(move_uploaded_file($_FILES['file']['tmp_name'], $path)) echo "Upload success";
else echo "Upload failed";
}
else echo "No temp file";
}
You had to change something... because $_FILES['file'] should be undefined in code you gave. No file is sent at all by most browsers in the case when file input has no name attribute in HTML form.
Simply change:
<input type="file">
to
<input type="file" name="file">
You need to add a "name" attribute to your <input type="file">. The name you give to the input will be the index of the array element in $_FILES that contains the uploaded file data.
In Php I am doing a file upload with some data insert
My form looks like this
<form id="formID1" method="post" action="'.$_SERVER['REQUEST_URI'].'" enctype="multipart/form-data">
<label for="">'.$this->l('Add Store Type').'</label>
<input type="text" name="store_type" />
<label for="">'.$this->l('Upload Your Custom Icon').'</label>
<input type="FILE" name="custom_icon" id="custom_icon" />
<input type="submit" name="store_config" value="'.$this->l('Add Store Configuration').'" class="button" />
<form>
For uploading images I am doing a folder with 777 permission on it.
$uploaddir=dirname(__FILE__)."/storeconfig";
if(!file_exists($uploaddir)) {
mkdir($uploaddir, 777, true);
}
$tmpName=$uploaddir."/";
Now for inserting values I am doing this code.
if(isset($_POST['store_config'])) {
global $uploaddir;
$custom_icon_name = time().$_FILES['custom_icon']['name'];
$allowed_extensions = array('gif','jpg','png');
$path_info = pathinfo($custom_icon_name);
if( !in_array($path_info['extension'], $allowed_extensions)) {
echo "Incorrent file extension, Please Use png,jpg or gif files only";
} else {
if(mysql_query('INSERT INTO `'._DB_PREFIX_.'store_config` (`store_config_id`,`store_type`,`custom_icon`) VALUES ("","'.$store_type.'","'.$custom_icon_name.'") ')) {
if(move_uploaded_file($_FILES['custom_pin']['tmp_name'],$tmpName.$custom_icon_name)) {
echo "Settings updated successfully";
} else {
echo "You Have Some Errors";
}
}
}
}
Here the values are storing into the database successfully after doing click on button Add Store Configuration but still I am getting the error You Have Some Errors which I have set in my form.It should show Settings updated successfully. Can someone tell me how this is error is coming?