move_uploaded_file - Failed to open stream: Invalid argument PHP - php

my web directory is at ip 192.168.120.3 , when I upload an image, I want the image to be stored in a different folder from my web directory, for example saved at ip 192.168.120.4, how do I do that?
This is my code :
$newname = $getFileName."-".date('YmdHis',time()).mt_rand().'.jpg';
$target = '\\\\192.168.164.5\\Common Pict\\'.$newname;
$tmp = $val['tmp_name'];
move_uploaded_file($tmp, $target);
Error :
move_uploaded_file(\192.168.164.5\Common Pict\m-20221206105651907307421.jpg): failed to open stream: Invalid argument
Code working if i upload an image to web directory:
$newname = $getFileName."-".date('YmdHis',time()).mt_rand().'.jpg';
$target = '\\\\192.168.120.3\\Common Pict\\'.$newname;
$tmp = $val['tmp_name'];
move_uploaded_file($tmp, $target);

Related

copy() fail to copy image from url to directory

I'm trying to copy an image from a URL to my server, but it doesn't save. Below is my code
$year ='2018';
$make = 'jeep';
$model = 'grand-cherokee';
$url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
$tmp_filename = explode('/images/auctions/', $url);
$filename = $tmp_filename[1].'.jpg';
//server path
$path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
$_path = pathinfo($path);
if (!file_exists($_path['dirname'])) {
mkdir($_path['dirname'], 0755, true);
}
//copy image to server
if (!is_file($path)) {
copy($url, $path);
//move_uploaded_file($url, $path);
}
The image URL in $url, doesn't have a file extension, but the image is a .jpeg
i tried both copy() and move_uploaded_file() but failed.
when i use copy() i get this error
copy(c:\xampp\htdocs\web\frontend/www/storage/auction/us/2018/jeep/grand-cherokee/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832.jpg): failed to open stream: No such file or directory
I also tried with directory 2018/jeep/grand-cherokee exist and without too.
what am is wrong here? Thanks
found the problem, Guess the filename was too long? I got this error when i tried manually saving the image from browser to desktop
a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832
The File name. directory name or volume label syntax is incorrect
I renamed the file here
$url = 'https://www.blabla.com/images/auctions/a540c388ce3ad8a480c62212abb7a895263da16689931dc34a2f9cdd3a2c7fcf68e22b87b79f9829c398a1e324df4791549f8a2557586bae9d1c0a37166d2a79050750ce2e20138a54bb53c47d631bb60c4c2bae6320f15fe88e83c2150d111ef616d2ffc2d80db947d0942b5e16d6d744aa2696611eac9987fe21fc662bbe3ca08dbdb3abf4c0c8b81bcf721dae19560b6365cf0f3d3a0629caa3f75ed52a43fb9832';
// $tmp_filename = explode('/images/auctions/', $url);
//$filename = $tmp_filename[1].'.jpg';
$filename = $year.'-'.$make.'-'.$model.'.jpg';
//server path
$path = str_replace(' ', '-', strtolower(Yii::app()->basePath.'/www/storage/auction/us/'.$year.'/'.$make.'/'.$model.'/'.$filename));
and it works now.

Read single file from ZIP within a directory

I have a ZIP file (with a VPK extension) and I wish to extract a file that is within a directory of the zip file. The uploaded file uploads correctly. Here is my current code. but unfortunately it throws up an error.
$hbid = substr(md5(time()),0,16);
mkdir("pkg/".$hbid, 0700);
mkdir("pkg_image/".$hbid, 0700);
$target_dir = "pkg/" . $hbid . "/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
...
FILE UPLOADING CODE HERE
...
ERROR -> $handle = fopen('zip://./'.$target_file.'#/sce_sys/icon0.png', 'r');
$result = '';
if($handle){
while (!feof($handle)) {
$result .= fread($handle, 8192);
}
fclose($handle);
$file = fopen("pkg_image/".$hbid."/icon0.png");
fwrite($file,$result);
fclose($file);
The error code is this:
fopen(zip://./pkg/0152cc9c0c52da70/4rows_1_1.vpk#/sce_sys/icon0.png): failed to open stream: operation failed
I've never extracted a file this way before but looking at other answers related to this, they all extract a file from the root of the zip, but the file I need is in a subdirectory of the zip file. I'm not entirely sure what I am doing wrong though.
Thanks.
Figured it out. The correction is to replace the /sce_sys with #sce_sys. The initial / is not required for a directory.

Image Upload in CakePHP on localhost

i am getting an error in uploading image into folder. there is an error occured during uploading that image.
my Controller Code is (cakeplus is my root folder ex: htpp://localhost/cakeplus) :
$directory = "http://".$_SERVER['HTTP_HOST'].'/cakeplus/pics';
if(!is_dir($directory)) {
mkdir($directory,0777);
}
$files_array = $this->data['Photo']['path'];
//pr($files_array); die;
if(isset($files_array['name']) && $files_array['name']!='') {
$filetype = $files_array['type'];
$filesize = $files_array['size'];
$filename = $files_array['name'];
$filetmpname = $files_array['tmp_name'];
$file_type = explode('.',$filename);
$ext_name = array_reverse($file_type);
$final_file_title = $ext_name[1];
$file_name = Inflector::slug( str_replace( ".".$ext_name[0], "" , $filename ). '_' .time() ,'-' );
$newFileName = $file_name.'.'.$ext_name[0];
move_uploaded_file($filetmpname, $directory.'/'.$newFileName);
$requirementuploadData['Photo']['path'] = $file_name;
$this->Photo->create();
$this->Photo->save($requirementuploadData,false);
}
Error(s)(Warnings) :
Warning (2): move_uploaded_file(http://localhost/cakeplus/pics/wallpaper-1433586197.png): failed to open stream: HTTP wrapper does not support writeable connections [APP\Controller\PhotosController.php, line 31]
Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\xampp\tmp\phpA80D.tmp' to 'http://localhost/cakeplus/pics/wallpaper-1433586197.png' [APP\Controller\Photos
Look into the CakePHP Upload plugin - it will abstract away much of the work that goes into dealing with file and image uploads.
The error you are seeing is because you cannot use move_uploaded_file() to transfer from a file path (C:\xampp\tmp\phpA80D.tmp) to an HTTP URL (http://localhost/cakeplus/pics/wallpaper-1433586197.png).
If you don't want to use the Upload plugin, and would prefer to keep working with what you already have, I would start by changing the $directory path. Something like this might be more appropriate:
$directory = WWW_ROOT . 'pics';
This will contain the path to your ./yourapp/webroot/pics directory, which is also the location of http://yourapp.com/pics.
Check out the documentation for more predefined paths.
may be folder dont have permission to write an image.
you should to use cakephp upload component.
$this->Upload->upload($file,$destination,$name);

How to specify path for the folder in Windows

I have a form that lets the user to upload files to the server, but I did not know how to write the correct path for that folder in the server which I want the files to be store in, also how to get the path for specific file to download later.
The path in the server:
/public_html/upload_files
The error I am getting:
Warning: move_uploaded_file(upload_files/project_guidelines.pdf):
failed to open stream: No such file or directory in
D:\sites\dwts.com\public_html\website\creat.php on line 50 Warning:
move_uploaded_file(): Unable to move 'C:\Windows\Temp\php14AC.tmp' to
'upload_files/project_guidelines.pdf' in
D:\sites\dwts.com\public_html\website\creat.php on line 50 Error
uploading file
Code:
$len = count($_FILES['Attachment']['name']);
for($i = 0; $i < $len; $i++) {
$uploadDir = 'upload_files/';
$fileName = $_FILES['Attachment']['name'][$i];
$tmpName = $_FILES['Attachment']['tmp_name'][$i];
$fileSize = $_FILES['Attachment']['size'][$i];
$fileType = $_FILES['Attachment']['type'][$i];
$filePath = $uploadDir . basename($_FILES['Attachment']['name'][$i]);
$result = move_uploaded_file($tmpName,$filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
$uploadDir = 'upload_files/';
Where is the drive? Its Windows, so it needs a drive:
$uploadDir = 'c:/upload_files/';
You can't give a partial location to the second argument of move_uploaded_file...it needs a full path.

php file upload script to my website?

I am trying to create a PHP file to upload images to my website. I have the following code, but it is not working. Can anyone help me fix it so I do not get any errors? Is there any better way to do such a thing? I know it is old fashioned.
$uploaddir = "uploads/images";
$allowed_ext = "jpg, JPG, png, gif";
$max_size = "500000";
$max_height = "4000";
$max_width = "4000";
$extension = pathinfo ($_FILES['file']['name']);
$extension = $extension[extension];
$allowed_paths = explode(", ", $allowed_ext);
for ($i = 0; $i <= count($allowed_paths); $i++){
if ($allowed_paths[$i] == "$extension"){
$ok = "1";
}
}
if ($ok == "1"){
if($_FILES['file']['size'] > $max_size){
print "Your file is too big!";
exit;
}
}
if($max_width && $max_height){
list($width, $height, $type, $w) = getimagesize($_FILES['file']['name']);
if ($width > $max_width || $height > $max_height){
print "Your file width/height are too big!";
exit;
}
}
if (is_uploaded_file($_FILES['file']['tmp_name'])){
move_uploaded_file($_FILES['file']['tmp_name'], $uploaddir.'/'.$_FILES['file']['name']);
print "Your file was uploaded successfully :)";
}
else
print "Wrong extensin";
?>
When I run the script I get the following error:
Warning: getimagesize(1 (8).jpg) [function.getimagesize]: failed to open stream: No such file or directory in D:\Hosting\8923686\html\uploadedimages\upload.php on line 25
Warning: move_uploaded_file(uploads/images/1 (8).jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'D:\Temp\php\phpE5F5.tmp' to 'uploads/images/1 (8).jpg' in D:\Hosting\8923686\html\uploadedimages\upload.php on line 33
or
invalid file
Can any one please tell me where is my problem?
As for the first error, change getimagesize($_FILES['file']['name']) to getimagesize($_FILES['file']['tmp_name']).
As for the other two errors, make sure your script has permissions to write files to the folder where you're trying to move the uploaded file to.
Edit: Also try to use a full absolute path in $uploaddir instead of just uploads/images. Since you're using a relative path from a script that isn't located in the document root, it's possible that PHP is looking in the wrong directory.
getimagesize($_FILES['file']['name'] in line 25 is the first culprit: you meant getimagesize($_FILES['file']['tmp_name']
For the othe errors you need to check permissions of uploads/images - they are obviously off limits for the webserver user.

Categories