PHP file upload: No such file or directory - php

I've a big problem I'm working on for days now. My file upload in PHP does not work for the following reason.
Here are some parts of my code:
# Upload file1
move_uploaded_file($_FILES['ev_ap_file1']['tmp_name'], 'as-content/'.$_FILES['ev_ap_file1']['name']);
# Upload file2
move_uploaded_file($_FILES['ev_ap_file2']['tmp_name'], 'as-includes/css/'.$_FILES['ev_ap_file2']['name']);
Infos about the files
Array
(
[ev_ap_file1] => Array
(
[name] => test.php
[type] => application/octet-stream
[tmp_name] => /tmp/phpfQIobp
[error] => 0
[size] => 0
)
[ev_ap_file2] => Array
(
[name] => test.css
[type] => text/css
[tmp_name] => /tmp/phpnQqf5T
[error] => 0
[size] => 0
)
)
And finally the error message I get:
Warning: move_uploaded_file(as-content/test.php): failed to open stream: No such file or directory in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 47
Warning: move_uploaded_file(): Unable to move '/tmp/phpfQIobp' to 'as-content/test.php' in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 47
Warning: move_uploaded_file(as-includes/css/test.css): failed to open stream: No such file or directory in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 49
Warning: move_uploaded_file(): Unable to move '/tmp/phpnQqf5T' to 'as-includes/css/test.css' in /var/www/vhosts/steuben-schule.de/httpdocs/steu-admin/action/a_page.php on line 49
Dev Exit enabled.
I already tried a lot of solutions and nothing worked. I'm the owner of the directory and of course it exist.
Thank you for your help!
Greetings, Arne

Possible Cases
Make sure have folder exist or you have enough permission to create new folder you are trying to move your file.
Folder [You want to move] must have write permission.
Apache user must have access of /tmp or temporary directory (set in php.ini "tmp_path").
Make sure server is not running out of memory`
Make sure file is not bigger than max_upload_size parameter

Related

Problems when uploading image to server nginx php 7.4 UPLOAD_ERR_NO_TMP_DIR with same req

I'm trying to upload an image with postman to my server. The problem is that it sometimes works and sometimes it does not.
I'm using exactly the same request every time. Is this some kind of protection within nginx or php?
When it works:
[image] => Array
(
[name] => 9.jpeg
[type] => image/jpeg
[tmp_name] => /home/test/tmp/php9LDwAL
[error] => 0
[size] => 351162
)
When it fails:
[image] => Array
(
[name] => 9.jpeg
[type] =>
[tmp_name] =>
[error] => 6
[size] => 0
)
PHP 7.4.29
edit 1: found some error logs:
File(/tmp) is not within the allowed path(s): (/home/test/web/site.com/public_html:
/home/test/tmp) in Unknown on line 0
[Fri May 20 19:56:02.661312 2022] [php7:warn] [pid 15995] PHP Warning:
File upload error - unable to create a temporary file in Unknown on line 0
edit 2:
I think i found the problem.
When uploading a file to the server it uses sometimes
sys_temp_dir and sometimes upload_tmp_dir as upload_dir
i first changed sys_temp_dir to /home/test/tmp without luck. After changing it to /home/test/web/site.com/public_html/tmp/ it is now working, but still does not feel like a proper solution.
Can someone please explain why the upload occasionally uses different paths? sys_temp_dir / upload_tmp_dir

Warning: proc_open(): Unable to create temporary file

I am working on a local server and getting two warnings:
Warning: proc_open(): Unable to create temporary file. in C:\xampp\htdocs\ *** on line 159
and
Warning: proc_open(): cannot represent a stream of type TEMP as a File Descriptor in C:\xampp\htdocs\ *** on line 159
My code is:
$err_stream = fopen('php://temp','rw');
$stdout = fopen('php://stdout','w');
$process = proc_open(self::PHP_8_0_COMMAND,[
0 => array("pipe", "r"),
1 => $stdout,
2 => $err_stream
], $pipes);
I've searched for the solution but I found nothing. I tried to change
$err_stream = fopen('php://temp','rw');
to
$err_stream = fopen('php://memory','rw');
but then I'm still getting the second warning.
Ok, I found out the answer.
The temporary directory was actually full and I just had to delete most of the files stored there.
I'll try to implement a function deleting old temporary files.

ssh2_scp_send not accepting array variable more than 9

this is my php script, here iam trying to upload photos to other server using SSH in that when i scan directory and store it in an array.
then i try to run in terminal upto index 9 its uploading properly, issue come when index comes at 10...........................................................................................................................................
$username = 'root';
$password = 'Workflow#5992$';
$connection = ssh2_connect($host, 22);
ssh2_auth_password($connection, $username, $password);
$sftp = ssh2_sftp($connection);
// Specifying directory
$mydir = '/var/www/html/AcadWFReport/photos';
// Scanning files in a given directory in ascending order
$myfiles = scandir($mydir);
// Displaying the files in the directory
print_r($myfiles);
$c = count($myfiles);
//print_r($c);
for ($i = 2; $i < $c; $i++){
$fname= substr($myfiles[$i],0,8);
$ext= substr($myfiles[$i],9,3);
$b= substr($myfiles[$i],2,2);
$batch=20 . $b ;
$pid= substr($myfiles[$i],4,1);
$pname=$myfiles[$i];
$result1=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'');
$result2=ssh2_sftp_mkdir($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'');
$local_file = '/var/www/html/AcadWFReport/photos/'.$pname.'';
$remote_file = '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'';
echo 'Uploading...'.$pname.''.$i.''."\n";
$stream = file_exists("ssh2.sftp://$connection/var/www/html/Dev/File_upload/doc/byroll/".$batch."/".$pid."/".$pname."");
if($stream){
echo "File already Exits";
$new_name= $fname.'_'.date('dmY').'.'.$ext;
ssh2_sftp_rename($sftp, '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$pname.'', '/var/www/html/Dev/File_upload/doc/byroll/'.$batch.'/'.$pid.'/'.$new_name.'');
}
$result = ssh2_scp_send($connection, $local_file, $remote_file, 0644);
if(!$result) {
echo 'Error while uploading';
}
else{
echo 'Uploaded successfully!'."\n";
}
}
and my output is
below
[root#localhost AcadWFReport]# php transfer.php
Array
(
[0] => .
[1] => ..
[2] => ME18S010.JPG
[3] => MM17S001.JPG
[4] => MS12B043.JPG
[5] => MS15F001.JPG
[6] => MS16D003.JPG
[7] => MS19S034.JPG
[8] => MS20B036.JPG
[9] => MS21D003.JPG
[10] => MS21W002.JPG
[11] => MS21W004.JPG
[12] => MS21W005.JPG
[13] => MS21W007.JPG
[14] => MS21W008.JPG
[15] => MS21W009.JPG
[16] => MS21W010.JPG
[17] => MS21W011.JPG
[18] => MS21W012.JPG
[19] => MS21W014.JPG
)
Uploading...ME18S010.JPG2
File already ExitsUploaded successfully!
Uploading...MM17S001.JPG3
File already ExitsUploaded successfully!
Uploading...MS12B043.JPG4
File already ExitsUploaded successfully!
Uploading...MS15F001.JPG5
File already ExitsUploaded successfully!
Uploading...MS16D003.JPG6
File already ExitsUploaded successfully!
Uploading...MS19S034.JPG7
File already ExitsUploaded successfully!
Uploading...MS20B036.JPG8
File already ExitsUploaded successfully!
Uploading...MS21D003.JPG9
File already ExitsUploaded successfully!
Uploading...MS21W002.JPG10
File already ExitsPHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W004.JPG11
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W005.JPG12
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W007.JPG13
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W008.JPG14
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W009.JPG15
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W010.JPG16
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W011.JPG17
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W012.JPG18
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60
Error while uploadingUploading...MS21W014.JPG19
PHP Warning: ssh2_scp_send(): Failure creating remote file: Channel open failure (connect failed) (-21) in /var/www/html/AcadWFReport/transfer.php on line 60 ```
and after index 9 it not allowing any double digit number to it
You start SFTP session (ssh2_sftp), while you use SCP later for actual transfer (ssh2_scp_send). You definitely do not need the ssh2_sftp line; and it can actually be a cause of your problem.
While technically it is possible to have both SFTP and SCP sessions over one SSH connection, I would not expect PHP to support this. Though I'm not sure.
and I commented the SFTP part in my code and uploaded more than 50 file though ssh2
And its working perfectly fine all 57 files have been moved to remote server from local server

Error messages for uploading files

I keep getting these error messages when using a plug in "üsers ultra pro":
Warning: mkdir(): No such file or directory in /homepages/2/d644181673/htdocs/app646332697/wp-content/plugins/users-ultra-pro/xooclasses/xoo.userultra.customizer.php on line 227
Warning: move_uploaded_file(/homepages/2/d644181673/htdocs/app646332697/usersultramedia/custom_avatar_image/uultra_default_avatar_image_.jpg): failed to open stream: No such file or directory in /homepages/2/d644181673/htdocs/app646332697/wp-content/plugins/users-ultra-pro/xooclasses/xoo.userultra.customizer.php on line 230
Warning: move_uploaded_file(): Unable to move '/tmp/phpTloN3B' to '/homepages/2/d644181673/htdocs/app646332697/usersultramedia/custom_avatar_image/uultra_default_avatar_image_.jpg' in /homepages/2/d644181673/htdocs/app646332697/wp-content/plugins/users-ultra-pro/xooclasses/xoo.userultra.customizer.php on line 230
Warning: getimagesize(/homepages/2/d644181673/htdocs/app646332697/usersultramedia/custom_avatar_image/uultra_default_avatar_image_.jpg): failed to open stream: No such file or directory in /homepages/2/d644181673/htdocs/app646332697/wp-content/plugins/users-ultra-pro/xooclasses/xoo.userultra.customizer.php on line 234
I have checked all the permissions are set to 755 but it still won't work. Any ideas? Thanks :)
Background: I am running a multi user site on Wordpress (think pinterest) where users will be able to upload images/text.

CakePHP 2.x warning SplFileInfo due to cakeshell

I wrote a cakeshell script which I plan to use it using cronjob. while having run it manually (during testing), sometimes my site will throw SplFileInfo Warning e.g:
Warning: SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_file_map):
failed to open stream: Permission denied in /var/www/flat/lib/Cake/Cache/Engine/FileEngine.php on line 313
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list):
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/persistent/myapp_cake_core_method_cache) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): SplFileInfo::openFile(/var/www/flat/app/tmp/cache/models/myapp_cake_model_default_flat_list) [http://php.net/splfileinfo.openfile]:
failed to open stream: Permission denied [CORE/Cake/Cache/Engine/FileEngine.php, line 313]
Warning (512): _cake_model_ cache was unable to write 'default_flat_list' to File cache [CORE/Cake/Cache/Cache.php, line 309]
What happened? and how to fix this?
If I check the tmp directory on cache and persistent some of the file are under root permissions. Is this the cause?
[root#Apps103 persistent]# ls
total 40K
-rw-rw-r-- 1 apache 43 Apr 22 17:49 myapp_cake_core_cake_
-rw-rw-r-- 1 root 43 Apr 23 10:01 myapp_cake_core_cake_console_
-rw-rw-r-- 1 root 43 Apr 23 10:01 myapp_cake_core_cake_dev_
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_cake_dev_en-us
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_cake_en-us
-rw-rw-r-- 1 apache 43 Apr 23 10:26 myapp_cake_core_default_en-us
-rw-rw-r-- 1 root 4.3K Apr 23 10:01 myapp_cake_core_file_map
-rw-rw-r-- 1 root 4.3K Apr 23 10:01 myapp_cake_core_method_cache
I tried the solution from this link SplFileInfo::openFile(/app/tmp/cache/persistent/cake_core_cake_console_):failed to open stream:Permission denied in /lib/.../FileEngine.php line 293
I put this in bootstrap
Cache::config('default', array(
'engine' => 'File',
'mask' => 0666,
));
// short
Cache::config('short', array(
'engine' => 'File',
'duration' => '+1 hours',
'path' => CACHE,
'prefix' => 'cake_short_'
));
// long
Cache::config('long', array(
'engine' => 'File',
'duration' => '+1 week',
'probability' => 100,
'path' => CACHE . 'long' . DS,
));
but it does not work. Usually I clean up the whole tmp directory to remove the warning. then it will work fine again. I'm not sure why, but if I ran the shell, the warning error does not show, until the next day.
My tmp directory (flat/app/tmp) is under apache permission.
What happened?
The tmp folder is normally in the app dir and shared for all requests no matter the origin. A relatively common problem is for tmp files created via a web request to be read-only for console tasks and vice-versa.
If you run cli tasks as root, the tmp files will be created with the owner root - thereafter a webrequest (running by apache) may try to delete from or write to the tmp directory but be unable to modify the existing tmp folder contents as the files are owned by root.
how to fix this?
Either make cli and web requests use different tmp directories - or simply ensure that both cli and web users have write permissions to the tmp directory.
That means either defining the TMP constant before CakePHP does to a place other than app/tmp, e.g. in cake.php:
...
$ds = DIRECTORY_SEPARATOR;
define('TMP', '/tmp/myapp');
...
Or ensure the tmp folder is writable to both users, e.g.:
cd app
chmod -R 777 tmp

Categories