Warning: move_uploaded_file(): The second argument to copy() function - php

I'm working on a file upload method. But i suddenly start to get the following error, The only thing I changed was the file name. I reverted it back, but the error still persists.
Does anyone know how to solve this?
The error message is:
Warning: move_uploaded_file(): The second argument to copy()
function cannot be a directory in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Warning: move_uploaded_file(): Unable to move '/tmp/phpJki8OC' to '/' in
/hermes/bosnaweb14a/b1717/ipg.plantationkeyartcorn/kittyrescuetnr/docs/upload.php
on line 13
Fail
my php looks like this
<?php
$file_upload="true";
$file_up_size=$_FILES['file_up'][size];
$file_destination=$REQUEST['file_type'];
$file_new_name=$REQUEST['file_name'];
$file_name=$_FILES[file_up][name];
**$add="$file_destination/$file_new_name"; // the path with the file name where the file will be stored**
if($file_upload=="true"){
if(move_uploaded_file ($_FILES[file_up][tmp_name], $add)){
echo print_r($file_new_name);
}else{echo "Fail";}
}else{
echo $msg;
}
?>

Well, since you are using superglobal array $_REQUEST incorrectly (notice the underscore in the name), your $add variable evaluates to just a slash. Which is a root directory.
Hence the errors - it is a directory after all and your script probably don't have write access to it anyway (which is a good thing).

Related

mkdir(): Not a directory PHP

I'm creating a path with codeigniter on PHP and I'm getting this php error :
A PHP Error was encountered
Severity: Warning
Message: mkdir(): Not a directory
Filename: devotee/acc.devotee.php
Line Number: 81
Here is the line of code that php is complaining about
// Set cache settings
$this->_cache_path = $this->EE->config->item('devotee_monitor_cachepath') ? $this->EE->config->item('devotee_monitor_cachepath') : APPPATH . 'cache/devotee/';
$this->_cache_time = 60 * 60; // 1 hour
// Create cache folder if it doesn't exist
if(! is_dir($this->_cache_path))
{
mkdir($this->_cache_path, DIR_WRITE_MODE);
}
Also, in addition to checking what $this->_cache_path is resolving to as Bassem Samir mentions, make sure that your param to mkdir() does not have a trailing slash. For example,
mkdir("some_dir");
... Works
Whereas
mkdir("some_dir/");
... Doesn't work.
In other words, mkdir() will expect something to follow the slash: the sub folder. If not present, you'll get an error.
You should check the content of $this->_cache_path. It can be empty so mkdir throws warning message.

Can't reference POST variable at parts of the code in PHP

I have a bizarre problem that I believe should have a very simple solution but, for the life of me, I can't seem to understand what's going on.
I'm grabbing a POST variable and I first check if it is posted and echo it. It is.
However, when I try to form a variable that is a full path to a filename, it doesn't recognize it and I obviously can't open the file...
I do get a warning for an Undefined variable so that's where the issue is but I can't understand why I can check the value of that variable anywhere and it's properly set.
Here's the code:
if (isset($_POST['fid'])) {
echo 'fid = ' . $_POST['fid'];
}
I form a whole path (which I echo and it is fine) to the file I'd like to open:
$fnameonly = getfilename($_POST['fid']);
echo $fnameonly; //echos fine, getfilename() worked fine
$fname = $_SERVER['DOCUMENT_ROOT'].'/uploads/' . $_SESSION["username"] . "/" . $fnameonly;
echo $fname; //also echos fine, $fname should be properly set
Now in looking at the resulting HTML, I get 2 warnings:
Notice: Undefined variable: fid in C:\web\www\csv\translatebs.php on line <i>23</i>
This is refers to the following line from the code above:
$fnameonly = getfilename($_POST['fid']);
The second warning refers to simplexml not being able to open the file since the path is incomplete.
Warning: simplexml_load_file(C:/web/www/uploads/paulohgo/): failed to open stream: No such file or directory in C:\web\www\csv\translatebs.php on line <i>31</i>
I can echo $fnameonly just fine but the warning is there.
When I try to open the file the value from $filenameonly is empty, so the I only get the path to a folder but miss the actual file name.
I don't get what I'm missing here... thanks for any help.

Getting error after inserting 3 pages php data insert script

I have 3 php pages where details is supposed to be inserted to db in final php.. I am geetting these warnings below however data is inserted. I know these warning can be turned of by error reporting but doesnt look good to me go for that..
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/..): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
Warning: copy(): The first argument to copy() function cannot be a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 301
Warning: unlink(tmp_imgs/tmp_1011/.): Is a directory in /home/opterfhb/public_html/quest4home.com/search/add_edit_property_finish.php on line 302
I think I have defined it wrong way.. seeking help..
I am getting error on this section:
// Moveing temp images to property directory
if ($handle = opendir('tmp_imgs/tmp_'.$property_id)) {
while (false !== ($file = readdir($handle)))
{
//$file_ext = strtolower(substr($file, strrpos($file, '.') + 1));
copy('tmp_imgs/tmp_'.$property_id.'/'.$file, 'property_images/img_'.$property_id.'/'.$file);
unlink('tmp_imgs/tmp_'.$property_id.'/'.$file);
}
closedir($handle);
}
The error is clear: you cannot use that function to copy directories. However, you may not be aware that you're even trying to do this in the first place.
There are magical "files" in each directory which are themselves directories (. and ..), so if you're iterating over a directory and copying everything in it, you need to explicitly skip those two.

Renaming a file results in 'No such file or directory'

I've been trying to rename an image file using php using this code.
$user_id = $_POST[user_id];
$old_image_name = $_POST[old_image_name];
$image_name = $_POST[image_name];
rename('/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'', '/_img/user_memes/large/user_'.$user_id.'/'.$old_image_name.'');
The error I'm getting is-
"Warning: rename(/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg,/_img/user_memes/large/user_2/1524957_717357634955838_1917151587_n.jpg) [function.rename]: No such file or directory in /var/sites/o/oddmeme.com/public_html/_process/post_single_meme_edit.php on line 8"
The image is definitely there. I've tried removing the / at the start and trying a few different version but nothing has worked.
I've also set permission to 777 so that shouldn't be a problem.
First of all, you have an error in your code:
You are using twice the same path in your function with $old_image_name...
Fix that and tell us if it's working.

Warning: move_uploaded_file() failed to open stream

I am trying to upload file on ftp.
here is my code
$jname= "Accounts of Biotechnology Research";
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
move_uploaded_file($_FILES["submission_file"]["tmp_name"], "/Trade/upload/$jname/" . $dup_name ); // line 67
Trade is a folder inside public_html folder.
When i am uploading a file it gives me a warning like,
Warning: mkdir() [function.mkdir]: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 63
Warning: move_uploaded_file(/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf) [function.move-uploaded-file]: failed to open stream: No such file or directory in /home/my_username/public_html/Trade/upload.php on line 67
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phphZXp0O' to '/Trade/upload/Accounts of Biotechnology Research/76164762-sm.pdf' in /home/my_username/public_html/Trade/upload.php on line 67
First:
You have a space here mkdir('/Trade/upload/ '.$jname);. Suppose you should have mkdir('/Trade/upload/'.$jname); (same for is_dir)
Second:
Ensure that you can write into Trade/upload directory.
Third (and I suppose that is the real problem):
It looks like you are trying to upload into a directory with full path:
/home/my_username/public_html/Trade/upload/, but your code will try to create a directory with full path:
/Trade/upload/.
You need to change
if (!is_dir('/Trade/upload/ '.$jname)) {
mkdir('/Trade/upload/ '.$jname); // line 63
}
to
if (!is_dir('upload/'.$jname)) {
mkdir('upload/'.$jname); // line 63 (or maybe there should be Trade/upload, but suppose current working dir will be /home/my_username/public_html/Trade, so only upload/)
}
Another option is to force mkdir to create directories recursively:
mkdir('/Trade/upload/'.$jname, 0755, true);
But in that case, files will be uploaded into /Trade/upload/... instead of /home/my_username/public_html/Trade/upload/...
There are two things you should be aware of based on the error messages you received. I'm guessing /Trade isn't the root path on your machine since it's clear in the error that your actual path is /home/my_username/public_html/Trade/, so the first adjustment should be
$root_path = "/home/my_username/public_html/Trade/upload/";
The second adjustment I'd suggest is that you avoid pathnames with space in them:
$jname= "Accounts of Biotechnology Research"; //could be changed to
$jname= "Accounts_of_Biotechnology_Research"; //$jname = str_replace(" ","-",$jname) OR
$jname= "Accounts-of-Biotechnology-Research"; //$jname = str_replace(" ","-",$jname)
Finally take note of the space character on the following lines, they affect you final result:
if (!is_dir('/Trade/upload/ '.$jname)) { //AND
mkdir('/Trade/upload/ '.$jname);
}
Note the [space] between upload/ '.$jname in both strings.

Categories