I am having issues moving files once uploaded. The upload appears to pass ok with no errors reported. I have 777 on the folder to upload to. The system is wordpress and I have no idea what I am doing wrong.
It should be noted that the form is inside another form. The eventual outcome is to have an image upload (this form, which is inside the larger one) that will allow the user to crop the image and add tags, title description etc before submitting the second form. The final submission of the second form will post to a custom post type and that is working fine. just the moving files and jcrop I am concerned about.
can anyone see a typo in there?
I cannot.
<form method="POST" action="" enctype="multipart/form-data">
<label for="image_upload">Image Upload</label>
<input id="image_upload" type="file" class="text_input" value="" name="file">
<input id="image-upload" type="submit" class="button" value="Upload" name="upload">
<!-- <img id="image-upload" src="<?php echo get_template_directory_uri(); ?>/images/sago.jpg" alt=""> -->
<?php
// Process the upload
if (!empty ($_POST['upload'])) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 100000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "<div> Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br> </div>";
//set temp dir path
$path = $_SERVER['DOCUMENT_ROOT'];
$upload_dir = $path . '/wp-content/uploads/jcrop_temp/';
if (file_exists($path . '/wp-content/uploads/jcrop_temp/' . $_FILES["file"]["name"]))
{
echo "<div style='border: solid 1px #BF5738; color: #BF5738; padding: 1em;'> The File: <span style='color: black;'>" . $_FILES["file"]["name"] . "</span> already exists. Please rename the file before trying again. </div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
echo "Stored in: " . $upload_dir . $_FILES["file"]["name"];
echo "<div style='border:solid 1px #E1E1E1; max-width: 710px; text-align: center;'>
<img id='image-upload' src='" . "/wp-content/uploads/jcrop_temp/" . $_FILES["file"]["name"] . "'>
</div>
";
}
}
}
else
{
echo "Invalid file";
}
//end upolad if
}
?>
</form>
The issue is with this line:
move_uploaded_file($_FILES["file"]["tmp_name"], $_FILES["file"]["name"]);
You need to specify the uploaded directory in your second parameter, like so:
move_uploaded_file($_FILES["file"]["tmp_name"], $upload_dir.$_FILES["file"]["name"]);
OK, So it is a convoluted process. Simple enough outside of Wordpress but inside.... its a pain.
There were a few thing I needed to change, firstly the file size was in bytes not kb! Idiot!...(Thanks to Panama Jack for making me look at it again and reminding me to not assume things.)
Secondly, the move_uploaded_file() function does NOT work inside wordpress. Instead I cobbled together something out of this useful post:http://wordpress.org/support/topic/using-move_uploaded_file-in-a-plugin
$path_array = wp_upload_dir();
$path = str_replace('\\', '/', $path_array['path']);
$old_name = $_FILES["image_upload_path"]["name"];
$split_name = explode('.',$old_name);
$time = time();
$file_name = $time.".".$split_name[1];
move_uploaded_file($_FILES["image_upload_path"]["tmp_name"],$path. "/" . $file_name);
(Please note if you use this code you WILL need to know what you are doing as the references in the question and answer do not correlate.)
With this I was able to send the uploaded file to the uploads directory and generate the various image sizes that wordpress likes (80x80 thumb, medium, large etc).
Why WP doesnt allow move_uploaded_file is beyond be....anyone?
Either way, it is possible, just a pain. I hope this helps.
Other Resources I used to get it working:
http://cube3x.com/2013/03/upload-files-to-wordpress-media-library-using-php/
move_uploaded_file() wordpress plugin
Related
okay, so very very new to PHP, and trying to complete my first major assignment. I'm nearly finish however very stuck, and tutor/school have not been the most helpfull. I need to know how to solve this, in the easiest term so that i understand this for later. Thanks in advance.
question 2.
so i have to Add more images to the front page (index.php) banner and make it rotate through the images. Store the images in the "Images/banner" folder and dynamically grab them.
how i have been able to create a javascript code to create the anner rotation, i just can't work out the php to dynamically grab them.
below is the banner page that allows upload of banner to image.banner folder.
i also have another question relating to same file which is
question 2
There is a bug on the "editBanner.php" page. When you delete an image, that number is deleted. When a new file is uploaded, it is given the number of one more than the total of current banners. There is a major problem with this, e.g. if we have four banners: 1.jpg, 2.jpg, 3.jpg and 4.jpg. If we delete 2.jpg, we are left with 1.jpg, 3.jpg and 4.jpg. If we then uploaded a new image, the page would count the number of images (in this case three) and add 1, giving us a new file name of "4.jpg". We already have a file called 4.jpg, therefore, the old 4.jpg would be replaced. Find a way to fix this error so the images are never replaced
<?php
require_once("Includes/simplecms-config.php");
require_once("Includes/connectDB.php");
include("Includes/header.php");
confirm_is_admin();
$dir = 'Images/banner';
$confirmation = "";
if (isset($_GET["del"])) {
if (file_exists($dir . "/" . $_GET["del"])) {
unlink($dir . "/" . $_GET["del"]);
$confirmation = $_GET["del"] . " deleted. <br><br>";
} else {
$confirmation = $_GET["del"] . " doesn't exist. <br><br>";
}
}
$files = scandir($dir);
array_shift($files);
array_shift($files);
$newImage = COUNT($files) + 1;
if (isset($_GET["add"])) {
$confirmation = "Image successfully uploaded.";
}
if (isset($_FILES["file"]["name"])) {
$allowedExts = array(
"gif",
"jpeg",
"jpg",
"png"
);
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 50 * 1024 * 1024) //50mb (1024 * 1kb = 1mb, 50 x 1024kb = 50mb)
&& in_array($extension, $allowedExts)
) {
if ($_FILES["file"]["error"] > 0) {
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . "
kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
<br>";
move_uploaded_file($_FILES["file"]["tmp_name"], $dir .
"/" . $newImage . "." . $extension);
echo "Stored in: " . $dir . "/" . $_FILES["file"]
["name"];
header("Location: editBanner.php?add=1");
}
} else {
echo "Invalid file";
}
}
?>
<div id="container">
<div id="admin">
<?php
if (!empty($confirmation)) {
echo "<div id='confirmation'>" . $confirmation . "</div>";
}
for ($i = 0; $i < COUNT($files); $i++) {
echo '<div id="bannerImage">';
echo $files[$i] . ' - <a href="editBanner.php?del=' .
$files[$i] . '">Delete</a><br>';
echo '<img src="' . $dir . '/' . $files[$i] . '"width="200px" height="100px" /><br>';
echo '</div>';
$files[$i--];
}
?>
<style>
#bannerAdd {
float: right;
margin-right: 700px;
}
</style>
<div id="bannerAdd">
<form action="editBanner.php" method="post"
enctype="multipart/form- data">
<h3>Upload a New Banner Image</h3>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>
</div>
</div>
</div>
</div> <!-- End of outer-wrapper which opens in header.php -->
<?php
include("Includes/footer.php");
?>
I am new to PHP and I'm trying to make an upload script. But it doesn't work completely.
The thing that doesn't work is that when I have uploaded the photo it doesn't store the photo in the folder "uploads". (The folder location is: Applications > MAMP > htdocs > Marjolein)
Also I want to show the photo that has been uploaded in the browser, but this also doesn't work.
I work with a Mac and use MAMP to run my php code. Can you please help me so I can show the picture in the browser and that it will be stored in the folder "uploads"?
The code I have is:
uploader.php
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("://Applications/MAMP/htdocs/Marjolein/uploads/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
<?php
if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!='')
{
?>
<p><img src="uploads/<?php echo $_REQUEST['show_image'];?>" /></p>
<?php
}
?>
uploadform.html
<html>
<body>
<form action="uploader.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>
The webbrowser shows after I click on the submit button:
Upload: 0_8caab_996cc75d_orig.jpg
Type: image/jpeg
Size: 538.166992188 kB
Temp file: /Applications/MAMP/tmp/php/phptpCA8B
Stored in: ://Applications/MAMP/htdocs/Marjolein/uploads/0_8caab_996cc75d_orig.jpg
Try to echo the image after uploading: (Don´t know about the storage of the image)
echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
Place it here:
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]."</div>";
echo '<img src="://Applications/MAMP/htdocs/Marjolein/uploads/" . $_FILES["file"]["name"]" border=0>';
}
Maybe the path to the file is not correct. i think you can try this instead of your previous path:
move_uploaded_file($_FILES["file"]["tmp_name"],
"../uploads/" . $_FILES["file"]["name"]);
or if the folder "uploads" is located at in the same as the uploader.php you can use this:
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
I've been trying to do a photo upload using Php. What I am seeing when I do a submit is, the page keeps loading infinitely.
End result the photo is not being uploaded to the directory.
Here is the HTML Code :
<!DOCTYPE html>
<html>
<head>
<title>Photo Upload</title>
</head>
<body>
<form action="upload.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 Script :
<style>
.sucess{
color:#088A08;
}
.error{
color:red;
}
</style>
<?php
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$upload_exts = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($upload_exts, $file_exts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
// Enter your path to upload file here
if (file_exists("uploads/" .
$_FILES["file"]["name"]))
{
echo "<div class='error'>"."(".$_FILES["file"]["name"].")".
" already exists. "."</div>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "<div class='sucess'>"."Stored in: " .
"uploads/" . $_FILES["file"]["name"]."</div>";
}
}
}
else
{
echo "<div class='error'>Invalid file</div>";
}
?>
I've taken this code sample from Here.
I cannot understand what is going wrong here, but I think it has something to do with the path, but I am not sure.
Some help will be appreciated.
*I've edited the code and changed it to 'uploads/' .
It now works!
I checked it out use Relative Path Instead of absolute path then there will be no problem which ever environment you are working in local or server
eg:Instead of this C:\Users\Priyabrata\PhpstormProjects\FileUpload/uploads/
Use uploads/
It will work
when you host it on server some hosting providers will not provide the access so you have to change the folder permissions to writable and it will work cheers
See you are reffering like C:\\Users\\Priyabrata\\PhpstormProjects\\FileUpload/uploads/.Maybe this leads to the problem.
Replace it with the actual server link you use ..
I have tried your code and it works fine for me (I used different path since I am on Ubuntu). Try specifying your path without double slashes. (Like in the sample link you provided). Also make sure the folder exists ;)
I would so appreciate some help here please. I have been struggling without success for a full day to upload images to a shared host running Zeus (rather than apache--yes I know, they are changing!). The host blames the code yet wont tell me why "as they are not programmers" I have tried so many different version of the form script I am out of options. I have of course checked the upload limits on the php ini file configs (which I am not allowed to change by the host) and they are both 128meg. So it looks unlikely that is the cause. The outcome of the scripts is that we get to the final ''successfully loaded the file'' message but the files size loaded is zero (so there is nothing new in the target directory). I am relatively new to php so please do go easy on the jargon. Thank you.
Here are the two files>> First the form...
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" />
</form>
and the php file refered to by the form...
<?php
$target = "uploads/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size >350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded to..";
echo $target;
echo "..The uploaded file size is $uploaded_size";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}
?>
First make sure the uploads folder exists and that is has write permissions set to either 755 or 777
Plus, you have two unassigned variables, $uploaded_size and $uploaded_type, so that will fail.
You would need to use something like if ($_FILES["file"]["size"] <350000) etc. probably another reason why it's failing.
Give this a try, see if this works for you, it's what I use:
Modify $allowedExts = array("gif", "jpeg", "jpg", "png"); for permitted file extensions.
It will upload only if size is less than 350000
NOTE: Change <input name="uploaded" type="file" /> to <input name="file" type="file" /> see form under handler code.
<?php
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 350000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
if (file_exists("uploads/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"uploads/" . $_FILES["file"]["name"]);
echo "Stored in: " . "uploads/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
Also change your form to this to reflect the PHP handler:
<form enctype="multipart/form-data" action="anewupload.php" method="POST">
Please choose a file: <input name="file" type="file" /><br />
<input type="submit" value="Upload" />
</form>
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
i am trying to upload an image.
<input name="files" type="file" id="files">
<input type="submit" name="submit" id="submit" value="Add" />
After Posting Data,
<?php
if(isset($_POST['submit']))
{
$path="panel/images"."/".$_FILES["file"]["name"];
move_uploaded_file($_FILES["files"]["tmp_name"], $path);
}
?>
but it does not upload the file.
make sure that you have specified enctype in form tag
<form action="test.php" enctype="multipart/form-data" method="POST">
Change this:-
$path="panel/images"."/".$_FILES["file"]["name"];
to
$path="panel/images"."/". basename($_FILES["files"]["name"]);
You have used id="files" in form but you are using "file" in your php code.
i think it should be like
$_FILES["files"]["name"] not $_FILES["file"]["name"]
in your $path
YOU have error in
if(isset($_POST['submit']))
{
$path="panel/images"."/".$_FILES["files"]["name"];
//This Line Your element name is files not file
move_uploaded_file($_FILES["files"]["tmp_name"], $path);
}
According to wat I understood ur problem is with only file upload..So I have briefed the php and html file for you..Comments are there in the code and I have tried to keep things easy. You can get the idea about the code through the comments. Any more queries wud be appreciated.
//This is the php code for upload file..
<?php
//allowedExts variable is an array consisting of file types that can be supported.we are uploading image so image file extensions are used.
$allowedExts = array("gif", "jpeg", "jpg", "png");
//explode function breaks the string, here used to get the file extension, whenever
//a dot(.) would be found in the filename,say abc.jpg, the extension jpg would be
//retrived.
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
{
//if any error in file, display it
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
//else upload the file
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br>";
echo "Type: " . $_FILES["file"]["type"] . "<br>";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
//file will be uploaded in the upload directory..but also need to check whether
//the directory consists a same filename already or not..if it does contains
// a file say abc.jpg already..it would display the msg file already exist
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
//else it will upload it in upload directory, you can name the directory acc to you..
//but conventionally upload is used as the directory name
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
//php file ends here
//this is the html file..frame it according to wat u need
<html>
<body>
//the most important thing is that you should keep the
//enctype(encryption type)=multipart/form-data. This is mandatory for media files
<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>