Manipulating file in PHP without $_FILES - php

I'm trying to programmatically add a tint to an image. I've done this very easily with a form and the $_FILES array but I need to get the image from a URL, and the images I'm trying to use are on my own server if that is important. Here's a sample code of the working form function
$img = new Upload($_FILES['imgfile']);
Now all I need is something like $img = new Upload(fopen($image_path); But this isn't working... Please help me!!

In the following code, you are passing a $_FILES array structure to your Upload class:
$img = new Upload($_FILES['imgfile']);
The variable $_FILES['imgfile'] itself is an array, consisting of named members such as 'name', 'tmp_name' and others - information about the file that has been passed to PHP by the server receiving the uploaded file.
Your Upload class appears to be designed to handle this sort of structure. It will probably have been designed specifically for handling file uploads, so you may need to modify it a bit to allow it to be passed any file path as a parameter instead of this array structure.

Well, you can fake $_FILES array all right.
the only function you cannot fool with this array is move_uploaded_file() but it seems you don't need it anyway.
but, you know, nobody have an idea what your "new Upload" is.

Related

How can I pass an image inside a JSON Array through AJAX and save it with PHP?

I have this line:
move_uploaded_file($_FILES["img"]["tmp_name"], "uploads/" . "$img_name");
That used to work when I received the image through a direct POST (without JSON.stringify the content).
Now I'm sending the ajax with the image inside of an array stringified, so I'm getting the contents of the array in the PHP file in this way:
$unstringified = json_decode(file_get_contents("php://input"), true);
And then I use:
$title = $unstringified["title"];
$content = $unstringified["content"];
$img = $unstringified["img"];
The problem is that now the move_uploaded_file stop working (seems to be no error but the image doesn't appear saved in the folder anymore). I tried some options like these, but didn't work.
move_uploaded_file($unstringified["img"]["tmp_name"], "uploads/" . "$img_name");
move_uploaded_file($_FILES[$unstringified["img"]]["tmp_name"], "uploads/" . "$img_name");
Any idea to solve this? Should I "convert" the image in any format until putting it in the array I will stringify? or I have to get the image in another way in the PHP file?
Thanks a lot in advance.
Leandro.
I wanted to pass the image inside a JSON because always that I've tried to pass and image with other string variables with formdata, I've always got an error, even playing with all posible variables of dataType, contentType , Cache, etc, and researching through lot of answers here. I couldn't find a solution to use form data to pass all togheter.
What I found I can do is to pass all inside within a JSON object without getting errors, so that's why I wanted to receive the image in PHP under this format and save it in this way.
But after trying lot of ways, again, and couldn't find a solution for this, I gave up, and use 2 separate sends, one json object with all the strings inside, and one formdata with only the image (if I don't add another variables like strings, there's no problem).
I'm sure it's not the best way, but it's what I can do.
Thanks all!

Renaming input file before uploading with jquery and laravel

I want to rename input file to be uploaded before sending to laravel.
Basically, i found an another way to rename the file in laravel but in this question I want to rename the file before sending to laravel.
In my case, I'm using jquery upload file, and now I want the input file to be renamed before uploading it. I want to get the new file name that I used to insert in the hidden input text.
Is there any solution to solve this kind of matter?
By the way, thank you in advance! ^_^
Did you check this: File Docs
as per this doc you can do this as follow:
$request->file('photo')->move($destinationPath, $fileName);
where $fileName is an optional parameter that renames the file.
so you can use this like:
$fileName = str_random(30); // any random string
then pass this as above.

Getting mime type from uploading files - inconsistent

I've got a script, largely based on an example uploading PHP file from jQuery Uploader. It gets file type with the following code (it gets this $_FILES component)...
$fileType = (isset($_SERVER['HTTP_X_FILE_TYPE']) ? $_SERVER['HTTP_X_FILE_TYPE'] : $upload['type']);
Note; $upload['type'] comes from the $_FILES['files']['type'].
Now, this is fine - except for the fact that some files seem to have no fileType information from this. I can get more accurate responses from using file info and mimetype functions in PHP - but they don't work on $_FILES objects and I'm trying to do this check before I transfer the file to s3 so I don't really want to load it locally.
Can anyone advise if there's something I can to get more accurately report type from $_FILES or is it going to have to load locally in order to run these alternative PHP functions?
finfo is the only way to do this. You cannot rely on information the client sends you, it is far too easy to fake from the client side.
There is no reason that it won't work with $_FILES, you would simply pass $_FILES['files']['tmp_name'] as the file path - this is still a valid file path, and you don't need to call move_uploaded_file() to access the data. Leaving the file in the temp location also has the advantage that it will be destroyed when the script is finished if you haven't done anything with it.

PHP GD Library and uploaded files

I'm working on a project where I upload an image (jpg) and manipulate it using the PHP GD library.
I know that I can use GD functions to edit an image resource (created from imagecreatefromjpeg()) but I was wondering if there was a way I could use the file uploaded in the $_FILES array directly with the GD library. One solution I thought of was saving the uploaded file, pushing it into imagecreatefromjpeg, then deleting it afterwards.
This seems cluinky though, is there a more efficient solution?
I'm still a bit new to PHP so I'm not sure as to how files are stored in the $_FILES array. I hope I'm making sense here. Thanks.
You can simply do this:
$img = imagecreatefromjpeg($_FILES['image']['tmp_name']);
// do gd operations on $img
imagejpeg($img, '/path/to/target');
You'll have to use imagecreatefrom in some form or another, and you can use it directly on the uploaded file. Then just save the result of your manipulations using imagejpeg. The uploaded file in tmp_name will we thrown away automatically.
Having said that, you should save the original somewhere. It's always good to have it around for later use.

PHP question Forum Attachment Feature Help

HI
I have a forum and I'm trying to think of how to do an "attachment" feature.
You know if you make a thread you can chose to upload a file and attach it in the thread.
Should I make a table called attachment with id of the file id in table files?? Whats the best way. And I want you to be able to upload more than 1 attachment. and if it's a picture show a little miniature of the picture.
How should I check if the file exist etc? How would you do this?
Sorry for my poor english
You question is too broad but I'll give you some pointers:
store the images on the disk, something like /uploads/--thread_id--/1.jpg, /uploads/--thread_id--/2.jpg and so on (this way you don't have to make any changes to your DB)
Regarding the upload process, validation and image resizing you can read more at (I recommend you read them in this order):
http://pt.php.net/manual/en/function.exif-imagetype.php -> image validation
http://php.net/manual/en/function.move-uploaded-file.php -> upload process
http://pt.php.net/manual/en/book.image.php -> image resizing & manipulation
Chacha's plan sounds good to me, but you have to be careful. Make sure the files that you save don't have any execution permissions and that the file isn't on a web-accessible directory on your server. I think you should put the upload directory in a directory higher than your web directory for security purposes.
Another possible way to save the files: save their binary code in blobs in the database. I'm not sure if there are any advantages to this method, but I haven't personally had to deal with file uploads.
Above all else, be careful with uploaded data!
I honestly would create a Column on the table of posts that says 'Attachments', and then do a comma delimited string of attachment file names
file1.png,file2.png,file3.png
then when you get it into PHP, simply explode it
$attachments = explode(',', $string);
and check for each file that you have already put in your upload directory:
foreach($attachments as $file)
{
if(!is_file($upload_directory.$file))
{
$error[] = $file . " is not a valid attachment";
// run cleanup script
}
}
To get the attachments, it is really simple code, but you need to validate and sanitize the incoming file.
foreach($_FILES as $array)
{
// Sanitize Here
die("SANITIZE HERE!");
move_uploaded_file($array['tmp_name'], $upload_dir);
}

Categories