In my PHP site I have a script to upload images, see the script below
$uploaddir = $root_path."images/uploaded_images/category/";
$small_file_name = trim($_FILES['cat_image1']['name']);
$small_file_len = strlen($small_file_name);
$small_file_ext = strtolower(substr($small_file_name,-4)); // select last 4 characters
$small_uploadfile = $uploaddir. $_FILES['cat_image1']['name'];
if($small_file_len>4 and ($small_file_ext==".gif" or $small_file_ext==".jpg" or $small_file_ext=="jpeg")){
if ($small_file_ext=="jpeg")
$uniqname = uniqid(rand()).".".$small_file_ext;
else
$uniqname = uniqid(rand()).$small_file_ext;
$thumb_filename1 = "thumb_".$uniqname; // store uniqname into database
$uploadfile = $uploaddir.$uniqname; //uncomment for local testing
if (move_uploaded_file($_FILES['cat_image1']['tmp_name'], $uploadfile)) {
chmod($uploadfile,0777);
list($width, $height, $type, $attr) = getimagesize($uploadfile);
$max_width = 276;
$max_height = 162;
$a = new Thumbnail($uploadfile,$max_width,$max_height,$uploaddir.$thumb_filename1,100,'');
$a->create();
}else{
$flag=1;
$frm_server_side_error= $frm_server_side_error."Error in uploading image,";
}
}
else{
$flag=2;
$frm_server_side_error= $frm_server_side_error."Image not in gif or jpg format,";
}
After uploading in to the server its can't be see (I am displaying the thumb image). So I checked the file permission of that image through the FTP and give read permission. Thus image displays. But I give the read and write permission to the category folder and it’s parent folders. How can view the image after uploading (default read permission to the uploading files ie thumb image.)
I noticed that the actual image have read permission but the thumb image doesn't have the read permission. I need to display only the thumb image.
You have used a class Thumbnail to create a thumbnail image. But I guess you have missed to change the file permission in the new thumbnail image. Please check the code in the create() function of the class Thumbnail
Your code formatted poorly here (half is in code format, half not), but it appears you have a comment before the snippet that would copy the file from the temp location to the final location - if that's the case, the file won't exist, and thus won't be viewable.
But I could be wrong - I'm happy to take a second look if you fix the formatting.
Related
I have absolute path of my uploaded image file and i want to get image file which i uploaded in my folder. So how can i get image file and how i can upload image.So i can perform image operation like watermark and rotate. So i have to get image file in code igniter.
i tried but it is not working
$upld_file = 'my absolute path';
$real_path = realpath($upld_file);
$img = base64_encode($real_path);
and i send $img parameter to upload image
I am getting "you did not select a file to upload"
C:\wamp64\www\codeigniter_project\assets/images/1551892667300.jpeg
This is my absolute path of my image
Thank You.
You don't need to get the absolute path of the image if you are storing the images inside a assets in the root of your project directory, You can access the image like this
echo base_url('assets/images/1551892667300.jpeg');
NOTE : you need to load the url helper first
You can try like this..
//define image path
$filename="C:/wamp64/www/codeigniter_project/assets/images/1551892667300.jpeg";
// Load the image
$source = imagecreatefromjpeg($filename);
// Rotate
$rotate = imagerotate($source, 90, 0);
//and save it on your server...
imagejpeg($rotate, "savedestinationpath.jpg");
That should be the file input name. eg: If your file browse input name is myFileInput then it should be $this->upload->do_upload('myFileInput')
Also make sure that you have set attribute enctype="multipart/form-data" for your form.
One nice example is here
I have a upload function implemented in my html code. In the following I'm saving the user input (image-upload) as an array in my SQL database:
if(isset($button_pressed))
{
$image = $_FILES['image']['name'];
move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . $_FILES['image']['name']);
....
}
Now I have a problem. In case when somebody doesn't upload a image, I want that a default image (no_img.png) will be displayed instead of nothing. For example, now the user upload images were shown but if a user doesn't upload a picture I want a default image output.
Does anybody know how to do that?
It should work like this:
if (/*no image uploaded*/) {
//set default image;
} else {
//set user uploaded image;
}
Thank you!
Due to the fact that the move_uploaded_file function returns a boolean based on whether or not the file you passed to it was actually uploaded and moved, you can do it in a very short method as follows:
if(move_uploaded_file($_FILES['image']['tmp_name'], 'images/' . $_FILES['image']['name'])) {
// Show uploaded image (HTML to draw the uploaded image by its path)
} else {
// Show default image (HTML to draw the default image by its path)
}
This code will show the uploaded image both if the image was uploaded and once it's been moved, and if it wasn't uploaded it'll show the default
As excerpted from the PHP documentation:
If filename is not a valid upload file, then no action will occur, and
move_uploaded_file() will return FALSE.
If filename is a valid upload file, but cannot be moved for some
reason, no action will occur, and move_uploaded_file() will return
FALSE. Additionally, a warning will be issued.
Thanks
I am working on a project that resize images. My following code takes an image from upload directory, resizes it and save the output image but the problem is that I have to hard code image name.
I want to get image name automatically from upload directory. Please someone solve my problem.
<?php
include('resize_lib.php'); // resize_lib is the library that has functionality of how to resize the image
//focus on this line
$image_path = "upload/something.jpg";// hard coded image name
$resizeObj = new resize($image_path);
$resizeObj -> resizeImage(1536, 1024, 0); // width // height
$resizeObj -> saveImage("new.png", 100);
echo "done...";
?>
Try to get all images files from your uploaded directory .jpg or .png or .gif
$files = glob("upload/*.{jpg,png,gif}", GLOB_BRACE);
glob
Returns an array containing the matched files/directories, an empty
array if no file matched or FALSE on error.
You can then use foreach() loop to set your image name for $image_path. By the way you can also select only a single type of image e.g something.jpg
$files = glob('upload/*.jpg');
I want to know that how can I overwrite images when they uploaded to server in php. For example I uploaded a photo to a folder as soon as I upload another image it will take place of previous image. Image name is not same it may differ. Thanks
Delete the previous image before uploading the new one using:
$path = $_SERVER['DOCUMENT_ROOT'].'img/img.jpg';
unlink($path);
This code basically assigns the path of the image to $path and deletes the image using unlink($path);
Short version
When I try to run file_get_contents() with this link, 'http://s1.reutersmedia.net/resources/r/?m=02&d=20131205&t=2&i=817503382&w=&fh=&fw=&ll=700&pl=378&r=CBRE9B401AG00', it returns: "illegal: d - msg". Why is it that file_get_contents() works on most image link but not this one, and how can I make it work?
Details
Part of my webapp's functionality is to parse external html files for images, then allow the user to select a desired image, and automatically save a reduced-size version of the image to my server. My code works for 99% of cases, but for the remaining 1% I am unable to successfully get the image file onto my server in order to re-size it. The cases that don't work seem to all involve html elements with 'src' attributes that look like this:
http://s1.reutersmedia.net/resources/r/?m=02&d=20131205&t=2&i=817503382&w=&fh=&fw=&ll=580&pl=378&r=CBRE9B401AG00
as opposed to a more standard image path such as this:
http://www.wired.com/images_blogs/wiredscience/2013/12/keyes-wd.jpg
Below is the code that I use in order to get and save the external image once the user has selected it, where the variable $newFileName is equal to an img path string such as the ones pasted above:
$contentOrFalseOnFailure = file_get_contents($newFileName);
$byteCountOrFalseOnFailure = file_put_contents($filenameOut, $contentOrFalseOnFailure);
$fileName = basename($newFileName);
$fileTmpLoc = $filenameOut;
$fileSize = $byteCountOrFalseOnFailure;
$fileExt = pathinfo($fileTmpLoc, PATHINFO_EXTENSION);
list($width, $height) = getimagesize($fileTmpLoc);
if($width < 10 || $height < 10){
header("location: ../message.php?msg=ERROR: That image has no dimensions");
exit();
}
When the src is non-standard, the script doesn't make it beyond this point, ie i get the "That image has no dimestions" error. What can I do to save save these non-standard images?
If you're interested in JUST the image dimensions and nothing else about it, you could go with GD's imagecreatefromstring() without needing a temp on-disk file:
$img = file_get_contents($url);
$gd = imagecreatefromstring($img);
$width = imagesx($gd);
$height = imagesy($gd);
This has the downside of having to decompress the image into memory, however. You'd have to hope that the remote server doesn't sent over a ludicriously dimensioned image that doesn't exceed the PHP memory_limit upon decompression.
Ignore the URL, look at the Content-Type header in the response.