PHP rename function error - php

I am trying to rename a file after getting the file name using basename(). I am getting an error saying 'cannot find the file specified'.
My code is :
$target_dir = "uploads/";
$base_name = basename($_FILES["file_to_upload"]["name"]);
$target_file = $target_dir.$base_name;//specifies the path of the file to be
uploaded.
$image_extention = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
//rename the file
$rename_file_name = rename($base_name,'Image_1');
dd($rename_file_name);
When I run the code i get the following error:
Warning: rename(RageFace.jpg,Image_1): The system cannot find the file specified. (code: 2) in C:\xampp\htdocs\pdo\file_upload_handle.php on line 23
bool(false)

try
changing name while moving
move_uploaded_file($_FILES["file_to_upload"]["tmp_name"],$target_file);

Related

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.

Get file size codeigniter

This gives a Warning Message: filesize(): stat failed for http://localhost/wft/uploads/4_Sat_Sep_10_2016_16_18_52.pdf
$file_path = base_url().'uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
how to find file size of pdf.
You could do this without using the URI, instead using the path to the File.
<?php
// CHANGE __DIR__ TO FIT WITH THE PATH TO THE ROOT DIRECTORY HOLDING THE PDF FILE
$file_path = __DIR__ . '/uploads/4_Sat_Sep_10_2016_16_18_52.pdf';
$size = filesize($file_path);
PHP filesize() function give the file size.
$path = "uploads/your_file";
$size = filesize($path);
NB: Path should be a relative path.

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.

Categories