Today I am adding a custom code to wordpress page.php to manage form data including file upload. I just want to check the real page so I can give the path to upload the file in code.
I have this code to upload the file:
$target = "uploads/";
$target = $target . basename($_FILES['pass_doc']['name']);
//Here we check that $ok was not set to 0 by an error
move_uploaded_file($_FILES['pass_doc']['tmp_name'], $target);
$pass_doc = $_FILES['pass_doc']['name'];
But I don't know target path should I give I tried:
$target = "example.org/uploads/";
But its also not working for me. Please give any suggestions.
Thanks
The problem is, when you says uploads/, that will be always a relative path to your current directory. Use the wordpress wp_upload_dir() function. Check the documentation here
$upload_dir = wp_upload_dir();
$target = $upload_dir['baseurl'] . "/" . basename($_FILES['pass_doc']['name']);
Related
My aim is to download multiple files into the folder on my localhost. I am uploading them using the HTML form.
Here is the code (really sorry that I can't give a link to the executable version of the code because it relies on too many other files and database if anyone knows the way then please let me know)
foreach ($_FILES as $value) {
$dir = '/';
$filename = $dir.basename($value['name']);
if (move_uploaded_file($value['tmp_name'],$filename)) {
echo "File was uploaded";
echo '<br>';
}
else {
echo "Upload failed";
echo '<br>';
}
}
So this little piece of code give me an error:
And here are the lines of code:
The problem is that the adress is correct, I tried enterring it into my file directory and it worked fine, I have seen some adviced on other people's related questions that // or \ should be used instead, but my version works just fine! Also I have checked what's inside the $_FILES and here it is if that's required for someone trying to help:
Thank you very much if anyone could help!!
You are trying to move the file to an invalid (or non-existent) path.
For the test you will write
$dir = 'c:/existing_dir/';
$filename = $dir.basename($value['name']);
If you want to move the file to a folder that is relative to the running file try
$dir = '../../directory/';// '../' -> one directory back
$filename = $dir.basename($value['name']);
By starting your file path with $dir = '/'; you are saying store the file on the root folder, I assume of C:
Apache if correctly configures should not allow you access to C:\
So either do
$dir = '../';
$filename = $dir.basename($value['name']);
to make it a relative path or leave the $dir = '/'; out completely
I have created a simple script to upload file in my WordPress plugin using
wp_handle_upload
In database only link to this image is stored. I would like to delete this uploaded file when i delete the post which it is linked to, however using
unlink()
does not work due to link structure which looks like this:
http://localhost/wp-content/uploads/2016/10/image.jpg
Does Anyone know the way to remove "http://[ip]/" from path or any WordPress method to remove uploaded file
I would be grateful for help.
You can use get_home_path() to get the root directory. Then your code would be:
$url = 'http://localhost/wp-content/uploads/2016/10/image.jpg';
$path = parse_url($url, PHP_URL_PATH); // Remove "http://localhost"
$fullPath = get_home_path() . $path;
unlink($fullPath);
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
I am trying to upload pdf file using Yii's CUploadFile. In my Controller:
$model->recipe_file_url = CUploadedFile::getInstance($model, 'recipe_file_url');
$file = $model->recipe_file_url;
$model->recipe_file_url->saveAs('images\uploads\\' . $file);
It is supposed to save the file in uploads directory under images directory, but it uploads it in the root directory with this name images\uploads\$file. How to solve this? any help?
Well, i got it myself. It was simple, I tried:
$model->recipe_file_url->saveAs('images/uploads/' . $file);
and it worked!
So I have a form that allows a user to upload files. When they submit the file, I can get the file information such as name and tmp_name, yet the actually upload doesn't work. I don't get any PHP errors either. Below is my code, I think I just need another pair of eyes on it, as it was working a few days ago.
//Get the file name
$target_Dir = "temp/";
$tempName = $_FILES['file']['tmp_name'];
$target_file = $target_Dir . basename($_FILES["file"]["name"]);
$filename = pathinfo($target_file, PATHINFO_FILENAME);
//Get the password
$password = $_POST['password'];
//Store if the user wants the certificate to remain password protected
$passProtect = $_POST['passProtect'];
//upload the file to the server
move_uploaded_file($tempName, $target_file);
I need the file name without the extension for a later point in my code, in case you're wondering why I'm storing the file name without the extension.
I think you just did this mistake
$fileExtension = pathinfo($target_file, PATHINFO_FILENAME);
Try using this insted of that Code then only you will get the file name not the extention.
I had an unlink() further down in my script for some reason that I can't remember. It deleted the file that the user would upload right away.
Thanks for the extra set of eyes guys.