This is my folder layout on the server: http://gyazo.com/1331ac0aaaa444bdedad9e91a6dfe23d
I am trying to save an image into the "1" folder.
move_uploaded_file($file_tmp, $_SERVER["DOCUMENT_ROOT"]."/Backend/Sets/".$desired_dir."/".$file_tmp);
That is the line I am using to try to move the file I have uploaded.
I printed out the $SERVER["DOCUMENT_ROOT"] and it ends with "/hub" - which you can see in my screen shot.
The issue is that my image in saving inside the /Backend folder and not going further into the /Sets/1 folders.
All of my .php files are in the /Backend folder. So that may have something to do with it. I am really unsure.
Thanks,
Waq
EDIT:
Here is the block of code that I have changed it to on suggestion from another thread:
if(empty($errors)==true){
print("desired dir = ".$desired_dir);
if(is_dir("Sets/".$desired_dir)==false){
mkdir("Sets/".$desired_dir, 0700);
}
if(is_file("Sets/".$desired_dir."/".$Title)==false){
rename ($file_tmp,$Title);
move_uploaded_file($Title,"/Sets/$desired_dir/$Title");
I have made sure desired_dir = 1.
Still the same problem however.
Add some error checking:
$sets = $_SERVER["DOCUMENT_ROOT"]."/Backend/Sets/".$desired_dir."/";
if (!is_dir($sets))
throw new Exception("Not a folder: " . $sets);
move_uploaded_file($file_tmp, $sets . $file_tmp);
In this way you have a clearer picture of what's going on
Related
I know there are already many similar questions like this and I apologize in advance for adding to the file, but I am a little short on time to do research and I need quick help. I am trying to finish an overdue assignment and my image upload function is working perfectly when I add a product, but not when I update it. I have no idea why. My code to update the image is here:
require_once 'file-util.php'
// Check if the file exists before setting it
if (isset($_FILES['imageFile1'])) {
// Retrieve the name of the file based on what it was called on the client computer
$filename = $codeInput . '.png';
// Make sure the filename exists
if (!empty($filename)) {
// Store the temporary location of where the file was stored on the server
$sourceLocation = $_FILES['imageFile1']['tmp_name'];
// Build the path to the images folder and use the same filename as before
$targetPath = $image_dir_path . DIRECTORY_SEPARATOR . $filename;
// Move file from temp directory to images folder
move_uploaded_file($sourceLocation, $targetPath);
}
}
This is the exact same code that I have in my insert_product file.
And my file_util is here:
$image_dir = 'images';
$image_dir_path = getcwd() . DIRECTORY_SEPARATOR . $image_dir;
Everything else works perfectly, but it is just this little thing that isn't seeming to do anything, so it seems to me like there's a little detail I'm missing for this to work in update_product. Is there something else I need to do to get this to work, or is it something else I'm unaware of?
Edit: Turns out that I just forgot to set the encryption type in my add_product_form. If anyone else has this silly issue, double check your forms for this near the top of the body:
<form action="insert_product.php" method="post"
id="add_product_form"
enctype="multipart/form-data">
You need to check if your updating form tag has the proper enctype attribute value...
and please be aware to use more validation on the uploaded file, your checking for file name exists or not will always be true as you are setting a value for it in the previous line.
Apparently, my code was right but I just forgot to go "enctype="multipart/form-data" in update_product_form.php.
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.');
}
Previously, when I tried uploading image into the database, the image won't display. When I check the path in the db and in the folder, it is correct.
Correct path in db and folder.
And then when I tried to view the image that has been uploaded it says that I don't have the permission to view it.
I have also tried uploaded different photo extension and different photo viewer application and I still cannot view the image. Apart from that, I have tried
W3School PHP5 File Upload. Again same thing happen, I cannot view my image.
This is my code :
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location= $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"];
move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"]);
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
Why can't I view my image? Is it because of the document root? Or is it something else? Please help me thank you.
UPDATED :
Based on the image below, my code (as shown above) is inside the admin folder. The reason why I would like to save my images in /ehars/photos so that, every level of user, admin admin2 and user can view the same photo that has been uploaded. If you could advice me what is the best way to do in order to achieve my objective above. Thanks again!
If your URL scheme is not "file://", you should authorized your browser.
I remember that you can't easily link CSS and image to the local machine due to security reasons.
change your code into this
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location='/ehars/photo/' . $_FILES["image"]["name"]; //remove $_SERVER['DOCUMENT_ROOT']
move_uploaded_file($_FILES["image"]["tmp_name"], '/ehars/photo/' . $_FILES["image"]["name"]); // remove $_SERVER['DOCUMENT_ROOT'] .
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
why tou should change your code, because your server not gonna read windows path (c:/apache/htdocs/yourimagespath/yourimages.jpg); it should read (/images/yourimages.jpg), i asume htdocs is your root directory. and the result in your database is /ehars/photo/yourimages.jpg not c:/apache/htdoc/ehars/photo/yourimages.jpg.
hope it help you.
In my project I have a folder secure in root. The project package looks like:
application
secure
system
...........
Inside secure folder I am uploading some images on form submit using
$config1['upload_path'] = './secure/';
$ext = end(explode(".", $_FILES['thumb_image']['name']));
$config1['file_name'] = time().$_FILES['thumb_image']['name'];
$config1['allowed_types'] = 'jpg|png|jpeg|gif|bmp|jpe|tiff|tif';
$this->load->library('upload', $config1);
$this->upload->initialize($config1);
$this->upload->do_upload('thumb_image');
and it is working properly. Now while on editing the details, using another form, if I am uploading a new image instead of the current image file, I want to unlink the current one and then upload new file.
For this I am using the code:
unlink(base_url("secure/".$data['row']->videothumbnail));
I also tried with
unlink('/secure/'.$data['row']->videothumbnail);
where $data['row']->videothumbnail) is the current image file from database. New file is successfully uploaded. But old file is not getting unlinked. I have set the permission of secure folder to 777. But the images are uploaded with read only permission. Is it because of this, it is not getting unlinked?
Can anyone help me to solve this?
Thanks in advance.
Try this:
Set the permission dynamically using:
#chmod('./secure/'.$data['row']->videothumbnail, 0777);
then try unlink:
#unlink('./secure/'.$data['row']->videothumbnail);
Try echoing the path that you are providing to unlink function.
It should be something like this:
base_url()."secure/".$data['row']->videothumbnail;
I also had this issue even after setting the right permission on the folder. But the following code worked for me.
unlink(realpath(APPPATH . '../uploads').'/'.$ImageName);
Try to use $_SERVER['DOCUMENT_ROOT'] instead of base_url
$this->load->helper("file")
unlink(base_url('folder/file.ext'));
location:
\app\controller
\system\libraries
**folder\file.ext**
$unlinkUrl = "secure/".$data['row']->videothumbnail;
if(file_exists($unlinkUrl)){
unlink($unlinkUrl);
}
else{
echo $unlinkUrl." is not available";
}
I think you are just making a stupid mistake.
Firstly, the first param of unlink should be a relative path or absolute path, but base_url function will return you a path contains domain name, HOW CAN YOU DELETE A FILE ON REMOTE SERVER ?
Secondly, '/secure/'.$data['row']->videothumbnail here is not a relative path but a absolute path
YOU MUST change it into /the/absolute/path/to/secure/ or ./the/relative/path/to/secure/ (DO NOT MISS THE DOT)
use this to unlink
$oldthumb = "secure/".$data['row']->videothumbnail;
#unlink($oldthumb);
First Load the $this->load->helper("file") and then unlink it
unlink("secure/".$data['row']->videothumbnail);
if ($rowAffected > 0) {
if ($isMediaUpload)
if (file_exists('./uploads/' . $this->input->post('img_url')))
unlink('./uploads/' . $this->input->post('img_url'));
redirect('/admin/configration', 'location');
}
Though i came in late but someone might need this .
unlink(FCPATH."secure/".$data['row']->videothumbnail)
**FCPATH** - path to front controller, usually index.php
**APPPATH** - path to application folder
**BASEPATH** - path to system folder.
I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);