Change Image Background Colour When Image Upload via PHP - php

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.

Related

How to send file to $_FILE through html form?

Well pretty simple question.. But can't get it right for some reason.
What would be the html code to send a file to this?
move_uploaded_file($FILES["upload"]["tmpname"], $_POST["name"]);
Here's mine but when I used it and echo/vardump everything, I only have 'name' and not the file
<form action="uploader.php" method="post" id="myForm" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="upload" id="upload">
<input type="text" name="name" id="name">
<button name="submit" class="btn btn-primary" type="submit" value="submit">Upload File</button>
</form>
Thank you
i try to add a comment but i can't
first check if upload permission is on in php.ini
file_uploads = On
If it set to on check your upload directory which you added in uploader.php file and use if to check $_FILES['upload'] is empty
this is a simple code to uploader.php file
<?php
if(!empty($_FILES['upload']))
{
$path = "upload/"; /// directory to upload
$path = $path . basename( $_FILES['upload']['name']);
if(move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['upload']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
}else {
echo 'No file selected to upload ';
}

Why does not this code allow upload of audio files?

I want to allow the user to upload audio files to my website, but after writing the code it turns out that it is failing to upload audio files. I have tried other filetypes and it's working for them.
Here's the HTML form:
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="audioFile"/>
<input type="submit" value="Upload Audio" name="save_audio"/>
</form>
And here's the php code:
<?php
if(isset($_POST['save_audio']) && $_POST['save_audio']=="Upload Audio") { $dir='uploads/'; $audio_path=$dir.basename($_FILES['audioFile']['name']);
if(move_uploaded_file($_FILES['audioFile']['tmp_name'],$audio_path))
{
echo 'uploaded successfully.';
}else
echo 'Upload Failed';
} ?>
With other file types am receiving the echo "Uploaded successfully" but when I try an audio file i get "Upload Failed".

Uploading Image to server using direct link and without form in PHP

This code allow me to choose an Image and upload it:
<?php
if(isset($_FILES['image'])){
$errors= array();
$file_name = $_FILES['image']['name'];
$file_tmp =$_FILES['image']['tmp_name'];
if(empty($errors)==true){
move_uploaded_file($file_tmp,"uploads/".$file_name);
echo "Success";
}else{
print_r($errors);
}
}
?>
<html>
<body>
<form action="" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
I want to upload the image using a direct link to the target image and without using a form or file selector..
so the image could be uploaded as soon as loading the php file

PHP - Image Upload , move_uploaded_file not working?

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.

Trouble with uploading a file

I can't get any files to upload successfully, it's just going to echo 'error';
HTML:
<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value = "2000000">
Upload this file: <input name ="userfile" type="file">
<input type="submit" value="Send File">
</form>
PHP:
<?php
if ($_FILES['userfile']['error']>0)
{
echo 'Problem.';
exit;
}
$upfile='/uploads/'.$_FILES['userfile']['name'];
if (is_uploaded_file($_FILES['userfile']['name']))
{
if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
{
echo 'Problem: could not move file';
exit;
}
}
else
{
echo 'Error';
exit;
}
echo 'File uploaded successfully.';
?>
I'm sure it's something simple I'm messing up, but I've spent about an hour trying to find it. Thanks.
if (is_uploaded_file($_FILES['userfile']['tmp_name']))
tmp_name instead of name
$_FILES['userfile']['tmp_name'] is the name of the uploaded file. $_FILES['userfile']['name'] is just the name that the file had when it was on the computer of the user.
For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the client's machine $_FILES['userfile']['name'] does not work.

Categories