I'm building a php upload script for images, and it refuses to run at all.
First off, I've got a simple post form like this:
<form action="variables.php" method="post" enctype="multipart/form-data" >
<p>Event title : <input type="text" name="name" required></p>
<p>Description : <input type="text" name="description" required></p>
<input type="file" name="file" id="file"/>
<input type="submit" value="submit">
</form>
This then feeds the submitted fields to "variables.php", which looks like this:
<?php
require("myfunctions.php");
$title = $_POST['name'];
$description =$_POST['description'];
$img = $_FILES["file"];
imgput($img);
generator($title, $description);
?>
"imgput" and "generator" are functions from "myfunctions.php", but the problem isn't in "genrator", so here's what "myfunctions.php" looks like:
<?php
function imgput($img) {
if ((($img["type"] == "image/gif")
|| ($img["type"] == "image/jpeg")
|| ($img["type"] == "image/pjpeg"))
&& ($img["size"] < 500000))
{
if ($img["error"] > 0)
{
echo "Return Code: " . $img["error"] . "<br />";
}
else
{
echo "Upload: " .$img["name"] . "<br />";
echo "Type: " . $img["type"] . "<br />";
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $img["tmp_name"] . "<br />";
if (file_exists("../uploads/" . $img["name"]))
{
echo $img["name"] . " already exists. ";
}
else
{
move_uploaded_file($img["tmp_name"],
"../uploads/" . "event1.jpg");
echo "Stored in: " . "../uploads/event1.jpg";
}
}
}
else
{
echo "Invalid file";
}
}
?>
Any help would be great. I tried running test echos right after "imgput" begins, but it won't even run it.
You're missing a ( here:
echo "Size: " . $img["size"] / 1024) . " Kb<br />";
^^^ no matching ( available
Most likely you wanted this:
echo "Size: " . ($img["size"] / 1024) . " Kb<br />";
If you had error_reporting turned on, you'd have seen the syntax error:
PHP Parse error: syntax error, unexpected ')', expecting ',' or ';' in /home/marc/test.php on line 16
Related
I use PHP to upload file to my server.I can't find my file in files folder.I suppose My code is right, but the file php.ini should be configured before uploading the file.Who can help me ? Here is my html code:
<form action="upload_file.php?act=put&name=use_resource" method="post" enctype="multipart/form-data">
<label for="file">PUT:</label>
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
My upload_file.php code is:
<?php
if ($_GET["act"] == "put")
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
move_uploaded_file($_FILES["file"]["tmp_name"], "files/".$_FILES["file"]["name"]);
}
}
Your code is perfect and working, Did you give write access to 'files' folder?
I'm a beginner in php and now doing a project in php. I want to upload images(maximum four image files only)
.I used the following code to upload images.
<?php if(isset($_POST['submit'])) {
$count=count($_FILES["images"]["name"]);
for($i=0;$i<$count;$i++) { if ((($_FILES["images"]["type"][$i] == "image/gif") || ($_FILES["images"]["type"][$i] == "image/jpeg") || ($_FILES["images"]["type"][$i] == "image/pjpeg")) && ($_FILES["images"]["size"][$i] < 100000)) {
if ($_FILES["images"]["error"][$i] > 0) { echo "File Error : " . $_FILES["images"]["error"][$i] . "<br />"; } else { echo "Upload File Name: " . $_FILES["images"]["name"][$i] . "<br />"; echo "File Type: " . $_FILES["images"]["type"][$i] . "<br />"; echo "File Size: " . ($_FILES["images"]["size"][$i] / 1024) . " Kb<br />";
if (file_exists("public/images/".$_FILES["images"]["name"][$i] )) { echo "<b>".$_FILES["images"]["name"][$i] . " already exists. </b>"; } else {
move_uploaded_file($_FILES["images"]["tmp_name"][$i] ,"public/images/". $_FILES["images"]["name"][$i] );
echo "Stored in: " . "public/images/" . $_FILES["images"]["name"][$i] ."<br />"; ?> Uploaded File:<br> <img src="public/images/<?php echo $_FILES["images"]["name"][$i] ; ?>" alt="Image path Invalid" > <?php } } }else { echo "Invalid file detail ::<br> file type ::".$_FILES["images"]["type"][$i] ." , file size::: ".$_FILES["images"]["size"][$i] ; } } }?>
First: Please, learn to indent!
Your count is bad. You are counting the $_FILES['images']['name'] size, but you need to count how many $_FILES['images'] there are. So change your code like this (note the new position of [$i] keys):
<?php
if (isset($_POST['submit'])) {
$count = count($_FILES["images"]);
for ($i = 0; $i < $count; $i++) {
if ((($_FILES["images"][$i]["type"] == "image/gif") || ($_FILES["images"][$i]["type"] == "image/jpeg") || ($_FILES["images"][$i]["type"] == "image/pjpeg")) && ($_FILES["images"][$i]["size"] < 100000)) {
if ($_FILES["images"][$i]["error"] > 0) {
echo "File Error : " . $_FILES["images"][$i]["error"] . "<br />";
} else {
echo "Upload File Name: " . $_FILES["images"][$i]["name"] . "<br />";
echo "File Type: " . $_FILES["images"][$i]["type"] . "<br />";
echo "File Size: " . ($_FILES["images"][$i]["size"] / 1024) . " Kb<br />";
if (file_exists("public/images/" . $_FILES["images"][$i]["name"])) {
echo "<b>" . $_FILES["images"][$i]["name"] . " already exists. </b>";
} else {
move_uploaded_file($_FILES["images"][$i]["tmp_name"], "public/images/" . $_FILES["images"][$i]["name"]);
echo "Stored in: " . "public/images/" . $_FILES["images"][$i]["name"] . "<br />";
?> Uploaded File:
<br>
<img src="public/images/
<?php echo $_FILES["images"][$i]["name"]; ?>" alt="Image path Invalid" >
<?php
}
}
} else {
echo "Invalid file detail ::<br> file type ::" . $_FILES["images"][$i]["type"] . " , file size::: " . $_FILES["images"][$i]["size"];
}
}
}?>
This code works assuming your html is something like this:
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
<input type="file" name="images[]" />
I am trying to create a form where a user can upload images. I'm using php for validation of this file to see whether it is an image file or not but I am getting an error
"Undefined index file.."
I can't understand what's wrong.. Please help
HTML code..
<html>
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
PHP code...
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
else
{
echo "Invalid file";
}
?>
There were two things wrong with your handler.
1) There was a missing closing brace above your last else condition
2) The following line contained characters (dots in tmp...) that didn't belong:
move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
Which was changed to:
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
Reformatted code, tested
<?php
if ((($_FILES["photo"]["type"] == "image/gif")
|| ($_FILES["photo"]["type"] == "image/jpeg")
|| ($_FILES["photo"]["type"] == "image/png"))
&& ($_FILES["photo"]["size"] < 1000000))
{
if ($_FILES["photo"]["error"] > 0)
{
echo "Return Code: " . $_FILES["photo"]["error"] . " ";
}
else
{
echo "Upload: " . $_FILES["photo"]["name"] . "";
echo "<br>";
echo "Type: " . $_FILES["photo"]["type"] . "";
echo "<br>";
echo "Size: " . ($_FILES["photo"]["size"] / 1024) . " Kb";
echo "<br>";
echo "Temp file: " . $_FILES["photo"]["tmp_name"] . "";
echo "<br>";
if (file_exists("users/" . $_FILES["photo"]["name"]))
{
echo $_FILES["photo"]["name"] . " already exists. ";
}
else
{
// error line for you to compare the error
// move_uploaded_file($_FILES["photo"]["tmp… "users/" . $_FILES["photo"]["name"]);
move_uploaded_file($_FILES["photo"]["tmp_name"], "users/" . $_FILES["photo"]["name"]);
echo "Stored in: " . "users/" . $_FILES["photo"]["name"];
}
}
} // this was the missing closing brace
else
{
echo "Invalid file";
}
?>
Added bonus: I added a few echo "<br>"; to seperate the fields on successful upload.
Which will appear like this:
Upload: image_test.jpg
Type: image/jpeg
Size: 26.16015625 Kb
Temp file: /tmp/phpifKd7I
Stored in: users/image_test.jpg
instead of on one line.
I am trying to upload a file using following code but the file is not getting saved in the desired location and no error is being popped out.
The php code is as follows`
<?php
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],"upload/".$_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
?>
The html code is
<html>
<body>
<form action="index.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
Check user permissions of the folder you want to store file to.
I have this form:
<form name="commentform" id="commentform" action="comment.php" method="post"
enctype="multipart/form-data">
Your Name:
<textarea maxlength="60" rows="1" cols="62" class="margin" name="name"
id="name"> </textarea> <br><br>
Submit Picture
<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>
This is the PHP to validate the picture (from W3Schools.com):
<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>
I am the submitting the form to the same page, so the PHP is executed as soon as the webpage loads. How can I make it load as soon as the form is submitted? Also, this script does not seem to be working.
You need to check if your form is submitted before you process the file upload:
if ( isset($_POST['pic'])) {
//save file here.
}
EDIT: It looks like your not referring to the right POST variable - you have a file element called 'pic' in your form but you are referring to $_POST['file'] in your PHP code which will not exist.
Also: If you are starting out with PHP, (IMHO) W3Schools.com is the worse place you can be - I've seen really bad examples of how code should NOT be written in there..
<?php
if( isset( $_POST( 'submit' ) ) ){ // Check form is submitted or not
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
}
?>
Add This To the Top of your page:
<?php $action = $_GET['action']; ?>
Your New Form:
<form name="commentform" id="commentform" action="comment.php?action=go" method="post" enctype="multipart/form-data">
Your Name: <textarea maxlength="60" rows="1" cols="62" class="margin" name="name" id="name"> </textarea> <br><br>
Submit Picture<input type="file" name="pic" id="pic" /> <br><br>
<input type="Submit" value="Submit" />
</form>
And the action script:
<?php
if (isset($action) && $action == 'go'){
if ((($_FILES["file"]["type"] == "image/gif") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/pjpeg")) && ($_FILES["file"]["size"] < 20000)) {
if ($_FILES["file"]["error"] > 0){
echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
}else{
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " already exists. ";
}else{
move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}else{
echo "Invalid file";
}
}
?>