How to specify path for the folder in Windows - php

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.

Related

move_uploaded_file - Failed to open stream: Invalid argument 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);

Converting Pdf file to images with Imagick and PHP

Program to load pdf image and at the same time convert them to jpg using Imagick.But couldnt convert and load it in Destination directory.
$name = $_FILES['file']['name'];
$fileName = substr($_FILES['file']['tmp_name'], 5).".".$ext;
date_default_timezone_set('UTC');
$fileDate = date('d.m.Y');
$fileSize = $_FILES['file']['size'];
$folder = $_POST['folder'];
$uploadfile1="$media_dir/$fileName";
$imagick = new imagick();
$imagick->readImage($uploadfile1);//line 149
$imagick->setImageFormat('jpg');
foreach($imagick as $i=>$imagick)
{
$imagick->writeImage($uploadfile1. " page ". ($i+1) ." of ". $pages.".jpg");
}
Error
Fatal error: Uncaught ImagickException: unable to open image
`/opt/ama/mediaFiles/phpe765pr.pdf': No such file or directory #
error/blob.c/OpenBlob/2701 in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php:149Stack
trace:#0
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/ama/modules/mediaFiles/uploadFile.php(149):
Imagick->readimage('/opt/gpssi/medi...')#1 {main} thrown in
/home/james/workspace/ama/1.1/userinterface/webfleet/gui/gpssi/modules/mediaFiles/uploadFile.php on line 149
You have a problem with the path of
/opt/ama/mediaFiles/phpe765pr.pdf
Make sure the path exists and the necessary privileges are given to all the folders in the path along the file to read it.

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);

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