Resizing remote images and saving it to the server - php

So i made use of this guide and was able to come up with this:
<html>
<head></head>
<body>
<form action="process.php" method="get">
<input type="text" name="image"/>
<input type="submit"/>
</form>
</body>
</html>
and process.php i came up with:
<?php
include("SimpleImage.php");
$imgName = $_GET["image"]; //assuming you used GET request and form submits to http://url/script.php?image=something.jpg
$image = new SimpleImage();
$image->load($imgName);
$image->resizeToWidth(250);
$image->save($imgName);
echo $imgName;
?>
However something's wrong. It's not saving the image :( I'm completely a PHP noob so i hope you can give some newbie friendly solutions. Thank YOu :)

check your folder/file permission if you are able to read/write on that directory.
If you have not set a path on where to save the new image, usually it writes to the same directory where your process.php is located.

Related

how upload image in php store in some directory and display it

simple question I am learning php so i want to give option to user that it uploads the image(using upload button ) and it save in my system directory and my program display it on browser
Try the following:
1) form.php
<html>
<head>
<title>Upload (TEST)</title>
</head>
<body>
<form action="upload.php" method="post">
<input type="file" name="file">
</form>
</body>
</html>
2) upload.php
<?php
if(pathinfo("yourdir/" . basename($_FILES["file"]["name"]), pathinfo_extension) != 'jpg') { // and so on...
move_uploaded_file($_FILES["file"]["tmp_name"], "yourdir/" . $_FILES["file"]["name"]);
}
?>
You don't even need to care about the extension, as the browser will display your file as an image if it recons it actually is an image, so you don't have to get mad with exestions. However, for security purposes, it is better to always check it is not a potentially malicious file (php, py, js ...)

No input file specified - php

I'm very new to php but worked a lot with javascript and html before. I'm hosting a server with apache on my own computer just for testing php. I installed php as cgi on it and everything worked fine. I did some testing with php just to get a feel for how it works, but when I tried to make a file upload form and read the file with php:
//uploadFile.php
<html>
<head>
</head>
<body>
<form action="uploadFile.php" id="form" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadFile">
<input name="submit" value="upload" type="submit">
</form>
<?php
echo $_FILES["uploadFile"]["name"];
?>
</body>
</html>
I suddenly got the message:
No input file specified
Only that! It didn't even said it was an error. I tried this very simple php script that worked before:
<?php
echo "Working";
?>
But I still got "No input file specified". Every php file I try gives me this error, wich indcates that the problem is't in my code. When I tried a normal .html file without php it worked. I have tried researching it and it seams like peaple hosting on godaddy gets this message, but I found no solution for me.
Do anyone know the solution or have any tips on how to go about solving this problem.
As per your originally posted code/question where you changed your question after this answer has been submitted:
PHP is case sensitive when it comes to POST/FILES variables, or any variables for that matter.
You have name="uploadfile" and then $_FILES["uploadFile"]
Change it to $_FILES["uploadfile"]
F is not the same as f.
Or keep $_FILES["uploadFile"] and change name="uploadfile" to name="uploadFile".
Either way, they both much match in letter-case.
Read the manual on uploading files:
http://php.net/manual/en/function.move-uploaded-file.php
Edit: (example)
Sidenote:
Make sure the folder exists and has proper write permissions. You may need to adjust the way the $uploads_dir = 'uploads'; is, depending on where you are executing the code from.
<html>
<head>
</head>
<body>
<form action="" id="form" method="POST" enctype="multipart/form-data">
<input type="file" name="uploadFile">
<input name="submit" value="upload" type="submit">
</form>
<?php
if(isset($_POST['submit'])){
$uploads_dir = 'uploads';
foreach ($_FILES["uploadFile"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["uploadFile"]["tmp_name"][$key];
$name = $_FILES["uploadFile"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
}
}
}
?>
</body>
</html>
"When I tried a normal .html file without php it worked."
Make sure that the code in my answer is used as a .php file and not .html.
You should also contact GoDaddy's technical support regarding this matter, and to see if PHP is indeed available for you to use.
Add error reporting to the top of your file(s) which will help find errors.
error_reporting(E_ALL);
ini_set('display_errors', 1);
Sidenote: Error reporting should only be done in staging, and never production.

How to retrieve <textarea> value with POST and save to a file?

On the page "index.php", I have the following html:
<form class="centerd" method="post" action="upload.php" enctype="multipart/form-data">
<textarea id="html" type="text" name="html-text"></textarea>
<input type="submit" value="upload"/>
</form>
On the page "upload.php", I have the following PHP code:
<?php
file_put_contents("/files/test.html", htmlspecialchars($_POST['html-text']));
?>
<h1>From the textbox:</h1>
<? echo htmlspecialchars($_POST['html-text']); ?>
I want the text from the textbox to show up after the <h1> tag, and I want /files/test.html to be created (/files/ already exists), and the text from the textbox to be put into the test.html file.
What actually happens, is whatever is in the textbox shows up after the <h1> tag, but /files/test.html is never created.
It looks like "/files/test.html" is an absolute path. Have you tried "files/test.html"?
I'm sorry that I cannot comment on what Brannon said, I simply don't have reputation for it yet. But to the point:
There are two possibilities:
As Brannon said -> your path seems to be wrong.
You don't have permission to write (create) files in your directory. This can, in my experience, be a common error for Mac users who haven't provided permission in their htdocs folder.
you forgot the php!
not: <? echo ?>
but: <?php echo ?>

Upload file doesn't neither try to upload

I have a very simple script:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<!-- this is up.php -->
<form action="up.php" method="post" enctype="multipart/form-data">
<input type="file">
<input type="submit">
</form>
<?php print_r($_FILES); ?>
</body>
</html>
When I select a file and I click on "Submit" the page is istantly reloaded and nothing happens.
I'm trying with this little script because in a more complex page with other fields everything works well except for the file form. I mean, the database is filled with all the informations provided by my form, but images are not uploaded.
The problem is that the page neither try to upload them, I mean, it should upload them (see the percentage on bottom of page) and then pass it to PHP.
In my case instead the page is istantly submitted and reloaded without the attempt of load pictures.
I have other scripts on my server that upload images, and them works.
Why this one does not work? Thanks.
Edit: live demo here:
http://allise.net/up.php
Actually your file needs to be named most probably.
Try
<input type="file" name="file">
or whatever is set in your up.php file
You need <form method="post" ...> for the form to work.
And the file input should have a name attribute.

Security implications of php upload system

I've made a basic looking PHP file load system, so far I can upload any file type, I haven't add any parameters just yet.
But what I want to know is with the method I'm using, what steps should I take to make the system secure? Is there another, more secure way of doing things, and any guides, tips or suggestions that may help with this system?
This is my code so far:
$upload_to = "img/company_logos/";
if($_POST)
{
if(!empty($_POST['upload']))
{
$upload_to = $upload_to . $_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $upload_to);
echo "Uploaded!";
}
}
?>
<html>
<body>
<form method="post" enctype="multipart/form-data">
<input type="hidden" id="upload" name="upload" value="1" />
<input type="file" id="file" name="file" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
One of the major things I've seen is the upload directory permissions are set to 777 this means anyone can read/write/execute this dir.
Thanks for the help.
Check the referrer
Restrict file types
Rename files
Change permissions
Login and Moderate
http://php.about.com/od/advancedphp/qt/upload_security.htm
And see this one
PHP Upload file enhance security

Categories