Have posted this over at wp.stackex also:
Hi I am trying to upload a file that is contained within a form I have on a page template in WordPress.
I cannot seem to upload the file to my specified directory, whether there is a special way of doing this or my file path information is not correct.
Here is my code: ( this is currently insecure for simple testing purposes, so please ignore.)
$target_path = dirname(__FILE__).'/event-submissions/';
$target_path = $target_path . basename( $_FILES['event-image']['name']);
if( move_uploaded_file($_FILES['event-image']['tmp_name'], $target_path) ) {
echo "The file ". basename( $_FILES['event-image']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
exit;
}
$target_path displays /homepages/4/d335638566/htdocs/dev/assets/themes/momentmag/event-submissions/FILENAME.FILE_EXT
So obviously I get the message "There was an error uploading the file, please try again!"
If anyone can shed any light on this - great!
Thanks
Your code looks fine to me. It looks like there is something you've overlooked that would help, but sadly you have no idea what it is. A rule of coding that I have is that, If something that you know should work, doesn't, it means that you've overlooked something - yet you've no idea what it is.
All I can say is that you can copy the file uploading code that the P2 Reloaded theme uses. It's more superior to yours, and it makes uploaded files actually attach themselves onto a blog post.
Related
I am trying to upload a client file to my server (from an html form using a "post" method), run a program on the $upldfile variable and then display the program results as downloadable links for the client.
My code is listed below and every time I run this I get the "file upload failed" notice.
Does anyone know if this is to with a permissions based problem or a server error or a code issue?
Thank you all in advance for any help offered
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$destination_path = getcwd().DIRECTORY_SEPARATOR;
$target_path = $destination_path . "uploads/" . basename( $_FILES["file"{["name"]);
$upldfile = move_uploaded_file($_FILES['file']['tmp_name'], $target_path);
if ($upldfile){
echo "<p>File upload success.</p>";
} else {
echo "<p>File upload failed.";
}
ANSWER:
Changing the permissions appropriately,
and also
modifying the php.ini file to allow larger file uploads.
The code itself in the original file was correct.
I am having a very difficult getting this working and I have yet to come up with a working scenario. Basically, this is a simple user-friendly admin which is supposed to upload an image. However, I can't get the upload to work. The filename is getting added to the database just fine but the image will not upload.
Here's the code as is:
function editMain($data){
array_pop($data);
$where = "main_id = {$data['main_id']}";
unset($data['main_id']);
//upload image
if ($_FILES['main_picture']['size'] > 1){ //if image deal with it
$data['main_picture'] = '/images/'.$_FILES['main_picture']['name'];
$uploadedfile = $_FILES['main_picture']['tmp_name'];
if (move_uploaded_file($uploadedfile, SITE_ROOT.$data['main_picture']))
echo "successfully uploaded {$data['main_picture']}<br />";
else
echo "failed to upload {$data['main_picture']}<br />";
}
Thanks for looking and thanks in advance for pointing me in the right direction!
SITE_ROOT needs to be a local directory, ie "C:\..." or "/home/...", it cannot be a URL structure. Change that to the local directory of where the file should be uploaded, check permissions, and you should be good to go.
I use it that way:
if(!is_dir($dir = $_SERVER['DOCUMENT_ROOT']."/fies_path"))mkdir($dir);
move_uploaded_file($_FILES['file']['tmp_name'],$src = $dir."/$file_name.ext");
I am trying to upload my php project into public server.
I made the image upload file when I create product or edit product.
It works in localhost, but when I move to public server, it is not working.
I think move_uploaded_file part does not working.
How can I change the link? or do I have to change anything?
When I see Filzilla, I can see remote site that it is '/www/eshopProject/inventory_images'.
And index file is '/www/eshopProject/storeAdmin'.
Do I have to change link like this?
I don't know how can I change the link.
Could you help me? uploading the image into public server is not working..
Is it any security issue? or something?
Please help me. Thanks.
--index.php--
$pid = mysql_insert_id();
//Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/product_$newname");
First of all check the permissions of the directory as mentioned in come of the comments.
If you have shell access "chmod 777 target_dir" or "chmod 707 target_dir" should be sufficient.
Second try to debug it using if's and the file_exists function(http://php.net/manual/en/function.file-exists.php).
Something like this.
$uploadedFile = $_FILES['fileField']['tmp_name'];
$destination = "../inventory_images/product_$newname";
if(file_exists($uploadedFile))
{
echo "file uploaded to temp dir";
}
else
{
echo "file upload failed";
exit();
}
if(move_uploaded_file($uploadedFile, $destination))
{
echo "upload complete";
}
else
{
echo "move_uploaded_file failed";
exit();
}
You can also check your current working directory by using the FILE or DIR constants(http://php.net/manual/en/language.constants.predefined.php).
Try this.
echo __FILE__;
echo dirname(__FILE__);
echo __DIR__;
Use the copy() method. For me it worked.
copy($tmp_file, Destination) or
copy($tmp_image, IMAGE_DIRECTORY . SAM . $product_image);
Make sure you have write file permissions set to the folder you are trying to upload too.
I recommend setting the folders to "755" permissions and retry. This would make the permissions a little tighter.
This question is a bit old but i recently faced a similar issue where even with permission 777 on the upload folder it wouldn't work.
The issue was that the SELinux (https://wiki.centos.org/HowTos/SELinux) was on enforcing mode so i had to change it to permissive mode and then the upload works perfectly.
I hope this can help someone facing this issue.
Here is a problem.
I have an HTML form with several fields in it.
One of the fields - 'Upload file'.
When I upload a file, everything works properly. But when I choose to submit the form without a file, it gives me the error message: "There was an error uploading the file, please try again". Looks to me that the script thinks that uploading a file is mandatory.
How do I change it?
Here is my PHP:
//File upload
// Where the file is going to be placed
$target_path = "uploads/";
// Add the original filename to our target path.
//Result is "uploads/filename.extension"
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
//End of file upload
Thank you!
You should check using the function is_uploaded_file
try adding the following condition before calling the function move_uploaded_file
if (is_uploaded_file($_FILES['uploadedfile']['tmp_name'])) {
The target path of move_uploaded_files should be a folder, not a file.
You should also check if the folder is_writeable and is_dir.
move_uploaded_file returns a bool, true or false, depending on the success of the operation.
A solution would be to check more rigorously, e.g. if a file was uploaded at all via is_uploaded_file function.
For proper working, the function is_uploaded_file() needs an argument like $_FILES['userfile']['tmp_name'], - the name of the uploaded file on the clients machine $_FILES['userfile']['name'] does not work.
I need to resize an uploaded image.
The class that resizes needs to get the location of the image to be worked with.
It returns the image in a variable.
However, when I try to get the path to the image, I get from $_FILES['profile_upload']['tmp_name'] the following: C:\xampp\tmp\php1C5.tmp
I don't get the actual file, even though the tmp folder contains it!
How can I get the actual filename? Another question - for how long are the files stored in tmp, and when do they get deleted?
By the way, does the Zend Framework have a good image manipulation interface?
You should complete the whole file upload setup with something similar and then the variable $_FILES['uploadedfile']['name'] will also contain the original file name:
$target_path = "uploads/";
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
}
To address your second point: Files are stored until the script they were uploaded to finishes.