I am trying to add a form to upload images in the user profile admin page in wordpress, i had tried this code before and it was working fine in a normal php page but when i tried it in this wordpress function it is not working.
Can someone help with this?
function image_up_gall(){
?>
<form action="#" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
add_action('edit_user_profile', 'image_up_gall');
add_action('show_user_profile', 'image_up_gall');
First of all edit_user_profile and show_user_profile action hooks don't have to save the image, you can just add a field there. So
function image_up_gall(){
?>
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
<?php
}
add_action('edit_user_profile', 'image_up_gall');
add_action('show_user_profile', 'image_up_gall');
It is because WordPress has its own form tag already, just make sure that it has enctype="multipart/form-data"
Second step, using personal_options_update and edit_user_profile_update you can save the form/upload imag, to do so, use this code:
function save_profile_fields( $user_id ) {
$target_dir = "uploads/"; // I recommend to use wp_upload_dir() to get the correct path
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
// here the image is uploaded and we can save it to user profile with:
update_usermeta( $user_id, 'profile_pic', $target_file );
}
}
add_action( 'personal_options_update', 'save_profile_fields' );
add_action( 'edit_user_profile_update', 'save_profile_fields' );
But I recommend you to use WordPress default media library to do that, there is a lot of code, so I better give you a link to the tutorial: https://rudrastyh.com/wordpress/customizable-media-uploader.html
you can try this bellow
if (isset($_FILES["file"]["name"])) {
$destination = $_POST["dir"];
$name = $_FILES["file"]["name"];
$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];
//echo $name;
//echo $tmp_name;
//echo $error;
move_uploaded_file($_FILES['file']['tmp_name'], $destination.$name);
}
Related
I am using this code to upload images with PHP
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
PHP:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"],
$target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
I want this: If I upload a image named "New York.jpg", the name gets changed to "New-York.jpg"
in your code , here :
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
add :
$target_file = str_replace(' ', '-', $target_file);
Replace
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
with this 2 lines,
$target_filename = str_replace(' ', '_', basename($_FILES["fileToUpload"]["name"]));
$target_file = $target_dir . $target_filename;
I need to create a simple web page via which you can upload a image file that goes to a directory on the server that is previously created.
Here's the code for the index.php file:
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
Here's the code for the upload.php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
The code is hosted on a free hosting server (not a paid one) and I created a dir called 'uploads' in the same subdirectory where both scripts are located.
In the php settings uploading seems to be on.
The index.php displays fine, I select an image and click upload, it loads for a second and then displays 'File is an image - image/gif.'
However when I got to the upload dir, there isn't a single file there.
What could be the issue? Thank you in advance.
You aren't moving the file to your server after uploading the temp file so it gets deleted
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
I want my users to be able to upload images to their account (my MySQL database). However, when I try to encode it and upload it, it appears that the file was never uploaded and is empty. I have checked the maximum upload size etc. in my PHP settings. Thanks in advance!!
$data = "";
if(isset($_FILES["up"])) {
$data = file_get_contents($_FILES['up']['tmp_name']);
$data = base64_encode($data);
$data = $connection->real_escape_string($data);
} else {
echo '<div style="position:absolute;height:100px;top:0px;left:0px;
border-top-right-radius:20px;border-top-left-radius:20px;
width:100%;background:white;z-index:100;"
>
<font style="color:#BB0000;font-size:2.2vw;">'.$_FILES['up']['error'].'</font>
</div>';
die('');
}
My HTML is: (And the form submits correctly)
<input type="file" accept=".jpg,.png,.jpeg" name="up" id="up"/>
Suggestion: store the images in a directory.
why not DB you ask?
read/write to a DB is always slower than a filesystem
your DB backups will become more time consuming
so, here is my solution.
STEP 1: create a directory userPhotos
STEP 2: create a form
<form action="upload.php" method="post" enctype="multipart/form-data">
Select your profile picture:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="Upload" value="Upload Image" name="submit">
</form>
STEP 3: create a file called upload.php which handles file uploads.
<?php
$target_dir = "userPhotos/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$newfilename = ;//assign unique user ID
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
if (move_uploaded_file($_FILES["fileToUpload"][$newfilename.$imageFileType], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";$uploadOk = 1;
}
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
if($uploadOK==1){
store the path of image in DB as "/userPhotos/".$newfilename
echo "uploaded photo : <img src='userphotos/".$newfilename."'">
}
//to display the image fetch the path using user ID as put it in src of img tag.
?>
Let me know if anyone has a better solution. thanks and Good luck.
PHP Code
$data = "";
if(isset($_FILES["up"])) {
$data = file_get_contents($_FILES['up']['tmp_name']);
$data = base64_encode($data);
$data = $connection->real_escape_string($data);
} else {
echo '<div style="position:absolute;height:100px;top:0px;left:0px;
border-top-right-radius:20px;border-top-left-radius:20px;
width:100%;background:white;z-index:100;"
>
<font style="color:#BB0000;font-size:2.2vw;">'.$_FILES['up']['error'].'</font>
</div>';
die('');
}
HTML Code
<form method="POST" enctype="multipart/form-data">
<input type="file" accept=".jpg,.png,.jpeg" name="up" id="up"/>
</form>
I've been struggling for the past couple days to submit an image through a form and have my php script save it in the designated directory. I've tried using $.ajax, I've tried using XMLHttpRequest, and now I've just copied a simpler script from here and tweaked it.
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file) evaluates to falseand I don't believe any warnings are given, so according to the documentation, there was a problem accessing the file.
The script response is
/private/var/tmp/phpwwBaBr
Sorry, there was an error uploading your file.
Also, the error code from echo $_FILE['fileToUpload']['error']; is 0, which means the upload is successful, yet still no file in ../includes/galleryImages/.
Here's the code:
<form action="archive_upload.php" id='file_form' method="post" enctype="multipart/form-data" accept-charset='UTF-8'>
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
$target_dir = "../includes/galleryImages/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
$uploadOk = 1;
} else {
$uploadOk = 0;
}
}
echo $_FILES["fileToUpload"]["tmp_name"].'<br>';
/* This is the line that returns false */
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo basename( $_FILES["fileToUpload"]["name"] ). " has been uploaded.<br>";
} else {
echo "Error uploading file.<br>";
}
?>
I'm new to HTML/PHP and I'm trying to create a simple php file upload page.
I have this as my HTML
<!DOCTYPE html>
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
I have this as my php:
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
I've uploaded both of these to the correct folder in my host (000webhost), yet when I check in my /uploads folder nothing is there. I've granted all my files read write and execute permissions to try and debug it - I'll learn about security later.
Any help would be greatly appreciated!
You must add this snippet of code:
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);
after that:
echo "File is an image - " . $check["mime"] . ".";
You need to move the uploaded file from the temp directory it was uploaded into, into your target directory. See the PHP docs of move_uploaded_files
With the PHP function move_uploaded_file(string $filename, string $destination) you can move the file to your desired path.