getimagezise Error pasing filename - php

I am using getimagesize() to get teh size of my image. I have image in my sub-directory when i am passing file name with sub-directory i ma facing an error:
There is no such file or directory
here is my Code:
<?PHP
function resize($img,$max_w){
$max_h=$max_w;
list($img_w,$img_h) = getimagesize($img);
$f = min($max_w/$img_w, $max_h/$img_h, 1);
$w = round($f * $img_w);
$h = round($f * $img_h);
return array($w,$h);
}
$filename="user_img/"."1256115556.jpg";
$resize=resize($filename,667);
$w=$resize[0];
$h=$resize[1];
?>
instead of this when i passing
$filename="1256115556.jpg";
file from my main directory the function is running perfectly. So please help me, How to pass file with sub-directory.

Pass your directory name inside getimagesize(). Because the file name is a String.
list($img_w,$img_h) = getimagesize("user_img/".$img);
Its working fine now.

If you're on a windows box then you might try a "\" and not a "/" in your path.

Related

PHP unlink(); No such file or directory

I searched everywhere for this problem and can't find the solution. I have this:
<?php
$file_name = $_GET['name'];
$file_delete = '../u/' . $file_name;
unlink($file_delete);
//header("location: $file_delete");
?>
unlink returns the error: No such file or directory, but if I try header("location: $file_delete"); it opens the file (picture in this case).
Where may I be wrong?
Get Absolute path first for the file to be deleted and check file exist before delete:
$file_name = $_GET['name'];
$base_dir = realpath($_SERVER["DOCUMENT_ROOT"]);
$file_delete = "$base_dir/your_inner_directories_path/$file_name";
if (file_exists($file_delete)) {unlink($file_delete);}
After some research, unlink() doesn't seem to allow you to use relative paths (with "../").
Here's an alternative:
<?php
$file_name = $_GET['name'];
$file_delete = dirname(__FILE__, 2) . '\\u\\' . $file_name;
unlink($file_delete);
?>
$file_delete here is the absolute path to the file you want to delete.
Reminder: / is used for Unix systems, \ for Windows.
PHP doc:
- http://php.net/manual/en/function.unlink.php
- http://php.net/manual/en/function.dirname.php
I also had same issue with my code. What I did to solve the issue is:
First execute:
var_dump($image_variable) // var_dump($file_delete) in your case.
It outputs: string(23)(my-image-path )
When I started counting string I just found 22 characters. I wondered where is the 23rd?
I checked and count carefully, at the end I found that there is space at the end of my image path. So I used php trim() function to remove white spaces. Like,
$trimed_path = trim($image_variable) // trim($file_delete) in your case.
Second: Now execute,
unlink($trimed_path).
OR CHECK LIKE
if(unlink($trimed_path))
{
echo "File Deleted";
}
else
{
echo "Error Deleting File";
}
Took me a couple of hours to figure out. As mentioned above unlink() is picky when it comes to paths.
Solution is:
1st) Define the path (this is how Wordpress does it btw):
define( 'ROOTPATH', dirname(dirname(__FILE__)) . '/' );
2) Do:
unlink(ROOTPATH.'public_html/file.jpg');

file upload error using codeigniter

I am tryin to upload file in mysql database using php with codeigniter framework.
Following my code. Its just saving first name of file in database but actual file in not storing at the given path.
My code is -
move_uploaded_file($_FILES["userfile"]["tmp_name"] , "uploads/diagnosis_report/".$_FILES["userfile"]["name"]);
$data['file_name'] = $_POST["userfile"]["name"];
I believe:
$data['file_name'] = $_POST["userfile"]["name"];
Should be:
$data['file_name'] = $_FILES["userfile"]["name"];
// ---------------------^
$upload_path$_POST["userfile"]["name"] is just the file name. If you want to save it with the path you need to do something like this:
// For relative path
$data['file_name'] = "uploads/diagnosis_report/".$_FILES["userfile"]["name"];
// For absolute path
$data['file_name'] = dirname(__FILE__)."/uploads/diagnosis_report/".$_FILES["userfile"]["name"];
Honestly what I would do is set an upload path variable first like so:
$upload_path = dirname(__FILE__)."/uploads/diagnosis_report/";
Then you can use it over and over again like so:
move_uploaded_file($_FILES["userfile"]["tmp_name"] , $upload_path.$_FILES["userfile"]["name"]);
$data['file_name'] = $upload_path.$_FILES["userfile"]["name"];
Hope this helps.

PHP Upload Script Not Functioning

The following code appears to upload ZIPs, but does not in fact create the document according to the naming convention I'm using:
// Upload ZIP
if((isset($_FILES['arquivo']['name'][0])) && ($_FILES['arquivo']['type'] == 'application/zip, application/octet-stream') && ($_FILES['arquivo']['size'] < 1000000)){
$arq = 'documento_'.uniqid().".zip';
$path = '/home/domain_name_here/www/documentos/';
$documento = $path.$arq;
move_uploaded_file($_FILES['arquivo']['tmp_name'], $documento);
chmod($documento, 0777);
}
Not entirely sure why this isn't functioning the way it should. Seems like the files upload to temporary and fail somewhere between there and the documentos folder.
Thanks in advance!
I would suspect that this line contains an error:
$arq = 'documento_'.uniqid().".zip';
You have double quote instead single quote wrapping .zip. It should be:
$arq = 'documento_'.uniqid().'.zip';

my gd is enabled but still the imagecreatefromstring still not working

i want to transfer image from localhost to a server using post. i have a problem on my php who catch the image here is my code:
$string = "iVBORw0KGgoAAAANSUhEUgAAAO0AAAA2CAIAAACk1ok0AAAAAXNSR0IArs4c6QAAAARnQU1BAACx
jwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAUJSURBVHhe7Z2hVuwwEIaRSBxIBA+ARCKRSCQS
icQhkUgkj4BEIpFIHgGJRMJ/T+6dk9uybZr8k06ys2rhJNOZf74mkzbt7nz7xxVoX4Gd9kPwCFyB
b+fYIehBAee4hyx6DM6xM9CDAs5xD1n0GJxjZ6AHBZzjNrL4/Px8enr6+PjYhrvVvVyfY+Tm8PBw
J/rgT6WEPT09hWPpHUIpgwcHB3B7d3c3wz7OgYHCsdqWv6enaU2OxwSLpnkJm87xx8cHzKoeIgOy
2S5vb2+Xl5fi9mz7cYNGIQ4hJ5JQm+PEseHq6iojYXGXz8/P29vbiRSWH6LQw9nuLy8vA/8vLi5m
e40b3N3dWR50p31LTFM9jl9fX+/v78P8OPggPV9fXxkZ+rULBrDr6+u9vb1NAqG6YB1Lz85g9kAs
Z2dnRJW4npdMFxRPFDlGJkDMzc0NFiibkDo6OirMTeIALw6ABopwqkYwmeDcDj7jzMeYhLFZ9YiF
xjvkeBFYhfKh+0TlkL5KKHeDaOHh4SGeTOzPHhiwOuR4E1j4P8YYlBYoMIhhj4u/JgawTdyjpo/n
riZmD1Rxwefj42Pi+bzIFL+uiMFCRYG6AiMKTtnYLSLHMNvNtVWMxKJMQ5OJrHlWnDr4HKecRlyO
S66tpnhbrY0AYXlJN1aDm808tXvg2IKOeeoPekkghWtfijPpRizovwLHuC7GjZxrLT1/9JaNBmLB
7docY6TBaiBEjuqZgoIFHbc5EAv61+ZY1uO43/j+/r7N6bdZaGZkZBs5lqUMLmtkSPZrFws6UmJp
NBALbtcejzVi1rBJ4XKpkRYDQaFowW3neClsiu0tALE0PGxaDG7jgvfSvsT2zjFRzFJTLXJ8cnIS
3CYWihk6OscZov3tEm8Npdx+a45juYSKVfvglm2+rFk9u+JYOFjlS+KO74k0Ncex7Kw4Pz/Pwo/W
yTmmMZ+447snjmVfHra40JDMMuQcF3FMKSckcc2Nx3Yc7orjrDPZUCc7WCSKYsdh5zgxZTWayWOw
uOtZ43jFx3COmeePHTULwZBl0+qXsVICwaNWFq4cB1eZPKUEr8Gchs2UWOhtcG8MO49DOOVXP+ju
DQxiXRtcxaMS2seate8cz0pUtYGR27wpMctWGTyoltJetY1zrCpvjnGZXpReqpTj0299TE2DzjEr
rTQ7strDF8soO8d/FKClHTX+vw/R5oqm4gd1LVfJpmRn8jSbe6UVrilBZ0VIaRCjnNJ+lTamZK/K
sdIK15SgLKSkusBeHJZNrh1TslflWGmFa0pQFivyXqzVt+BsisiU7FU5VopcySyLyDw78VPl627t
dY6HCigBp2Q2jz9iL3ntsc3VninZfTwmgjdjavYNjtg9F79a0/g9EeeYfPKYEnSC5ZL3wtc725KP
ZEp2MlLTIihFLkt7m3WkaFLyXniDLxFVymbyefRfwx44lm1iNuvI9MSEN+nv7+8LIrNfuBv5011F
S+eYfPIYryMXwYHGGeXHKjQ7x2SOrY0NS8EdtB+XH1j8haF6AvH6c9H2ciyFbGGmx92be5KCokBM
fPlTrktd2l6Og+4aisdPUrTyUNBSbqy1316O9TIRP0kR9MUUDLjpLwfp5jccynOhN7tm+MYvVTOc
oHQZozy72M9uUL8YpUjENaI3u2b42Q/HCB4oxz9hm41pSscMrb2LngJdcRzLhN8Okhffp3C5qI1G
ia+X422w/AO/8BouFT8hjwAAAABJRU5ErkJggg==";
that $string is my sample image
$img2 = base64_decode($string);
$img = imagecreatefromstring($img2);
if($img != false)
{
//this is the location and name you can use date time to create name if you want
//also put the path before the image if you want to save it to a folder ex. 'pics/image.jpg'
$path = "sample_upload/images/" . date("Y_m_d_H_i_s") . ".jpg";// put path here
$sampledate = date("Y_m_d_H_i_s");
echo $sampledate;
imagejpeg($img, $path.''.$sampledate.'.jpg');
//they can put db query here if they like to insert img path and email
}
the error is [31-Aug-2013 13:56:49 Asia/Manila] PHP Fatal error: Call to undefined function imagecreatefromstring()
the php version should be higher than 5

Changing Base Path In PHP

I need to change the folder that "relative include paths" are based on.
I might currently be "in" this folder:
C:\ABC\XYZ\123\ZZZ
And in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\ABC\XYZ\Source
And realpath('.') would = 'C:\ABC\XYZ\123\ZZZ';
If however, realpath('.') were "C:\Some\Other\Folder"
Then in this case, the path "../../Source/SomeCode.php" would actually be in this folder:
C:\Some\Source
How do I change what folder is represented by '.' in realpath()?
Like this:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
// Some PHP code here...
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
How can I change the folder represented by '.', as seen by realpath()?
The function chdir() does this.
For example:
echo ('BEFORE = '.realpath('.')); // BEFORE = C:\ABC\XYZ\123\ZZZ
chdir('C:/Some/Other/Folder');
echo ('AFTER = '.realpath('.')); // AFTER = C:\Some\Other\Folder
Use the chdir() function.
Change your current working directory with chdir()
http://us.php.net/chdir

Categories