PHP File Upload Help [duplicate] - php

<html><head><title>File Upload</title></head>
<body bgcolor = "lavender"><div align = "center">
<?php
if (isset($_FILES['file']) && move_uploaded_file(
$_FILES['file']['name']))
{
echo "<font color = 'green'>The file has been uploaded.</font>";
}
else echo "<font color = 'red'>There was an error uploading the file.</font>";
?>
</div></body>
</html>
This is my code.
I am trying to upload a file via a seperate form in a seperate webpage, using the method 'get'.
The code to the form as shown is here:
<form enctype="multipart/form-data" action = "fileupload.php" method = "get">
<input type = "file" name = "file"><br>
<input type = "submit" value = "Upload">
</form>
For some reason I keep on getting the error message - although I'm pretty sure I'm doing it right. This is my first time doing this, suggestions would be appreciated.

it should be tmp_name
if (isset($_FILES['file']) && move_uploaded_file($_FILES['file']['tmp_name'],'ftp/' . $_FILES['file']['name']))
and do not send it as GET
<form enctype="multipart/form-data" action = "fileupload.php" method = "post">
(changed get to post)

I'm guessing you would need to supply a destination in the move-uploaded-file function as an argument. If the move to the destination (in this case non-existent) cannot be accomplished, it would return false, which is why it would go to your else{} condition.

move_uploaded_file() requires a $destination parameter as it's second argument. This should be set to the path at which you want to save the file.

You cannot send the file through $_GET. You should use POST as your method for the form.

Related

PHP Post data not received

i'm currently working on a small script for my Homepage but i ran into a problem.
I Try to upload an Image, but it seems like the POST data from the form is not being received. What did i do wrong?
I already changed the post_max_size and everything in the php.ini.
These are the Errors i get:
"Notice: Undefined index: image in ...." & "Notice: Undefined index:
submit in ...."
<form method="POST" action="/eye/sites/handling/post.php" enctype="multipart/form-data">
<div class="fileUpload">
<span><i class="fa fa-folder-o" aria-hidden="true"></i> Bild wählen</span>
<input type="file" class="upload" name="image"/>
</div>
<input type="submit" value="Upload It!" name="submit"/>
</form>
<?php session_start();
error_reporting(E_ALL);
if (isset($_SESSION["login_stat"])) {
date_default_timezone_set('Europe/Berlin');
$config = "$_SERVER[DOCUMENT_ROOT]/eye/more/config.xml";
$xml = simplexml_load_file($config);
$picWidth = $xml->pic->width;
$picHeight = $xml->pic->height;
$fulldate = date('dmYHis');
if(isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
$typeCheck = $_FILES['image']['type'];
if ($typeCheck != "image/jpeg") {
$error = "Not a .jpg";
header('location: /eye/sites/post.php?stat=bad&error='.$error);
exit;
}
$file = $_SERVER['DOCUMENT_ROOT']."/uploads/".$fulldate.".jpg";
$type = "image/jpeg";
move_uploaded_file($_FILES['image']['tmp_name'], $file);
$file_thmb = $_SERVER['DOCUMENT_ROOT']."/uploads/!1A_thmb/".$fulldate.".jpg";
include "resize-class.php";
$resizeObj = new resize($file);
$resizeObj->resizeImage($picWidth, $picHeight, 'crop');
$resizeObj->saveImage($file_thmb, 100);
// header('location: /eye/sites/post.php?stat=good');
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No File');
}
} else{
// header('location: /eye/sites/post.php?stat=bad&error=No Data');
echo $_SERVER['CONTENT_TYPE'];
echo "<br>";
echo $_FILES['image']['tmp_name'];
echo "<br>";
echo $_POST['submit'];
echo "<br>";
}
} else {
header('location: /eye/index.php?stat=in');
}
?>
Edit:
The problem is definitely about my Localhost.
This whole thing is working fine on my Webspace, but on my localhost it's not working.
BUT: I'm not getting errors anymore, when is click on Submit it goes to the php file that should save the image, but nothing is happening. I just see a white Page.
But like i said, it runs perfectly on my webspace..
If this is running on your local machine, do a quick check to make sure your "php.ini" file is configured to allow file uploads.
php.ini
file_uploads = On
The codes look fine. Check if your form action is posting to the correct path and if I may suggest using a simpler approach to test your file upload function before making it more complex. Use the following to start testing.
if (isset($_POST["submit"])) {
if (file_exists($_FILES['image']['tmp_name']) || is_uploaded_file($_FILES['image']['tmp_name'])) {
echo "Upload is working!";
}
}
Keep us updated on your findings.
Perhaps this general information will help someone, as it helped me: a submitted form will only include fields that have defined 'name' attributes. 'id' is not enough.
The idea is that 'id' identifies an element in the DOM for use by JavaScript (either as a global variable or for use in document.getElementById(ID)), but 'name' identifies those elements whose names and values will be sent to the destination ('action') page.
So it makes sense that there are two different identifying attributes, used in two different ways.

Connection times out when trying to use HTML form and PHP to read from different files

I'm setting up a gallery where the content of gallery.php depends on the POST data sent to it. The page first retrieves the filename of entry at line number $pagenumber in CanvasList.txt, then it gets the content of that file. If no POST data was sent, it defaults to $pagenumber = 0. The page loads when it is accessed without sending POST data, but when I use the form, the connection times out. Why is this? Here's some of my code:
<div class='formbox'>
<form class='navigator' method='POST' action='https://www.mydomain.com/gallery.php'>
<input type='text' name='pagenumber' value='pagenumber'>
<input type='submit' id='gotopage' value='Go'>
</div>
<?php
$input = $_SERVER['REQUEST_METHOD'];
if ($input == 'POST') {
$pagenumber = (int)$_POST['pagenumber'];
} else {
$pagenumber = 0;
}
$list = "gallery/CanvasList.txt";
$lines = file($list, FILE_IGNORE_NEW_LINES);
$filename = $lines[$pagenumber];
$canvasHandle = fopen('gallery/' . $filename, 'r');
//getting and processing content (works)
I tried changing the default value of $pagenumber, but same result.
changed
action='https://www.mydomain.com/gallery.php'
to
action='gallery.php'
That solved it. I'm not allowed to accept my own answer before in two days, on 23/1/14. If someone else is allowed to, I encourage them to mark this Answer as accepted.

How to i post image id to php along with $_FILE

index.html with form tag
<input type='file' name='picture' id='".$row['sfname']."'onchange='javascript:ajaxFileUpload(this);'/>`
ajax code
function ajaxFileUpload(upload_field)
{
// Checking file type
var re_text = /\.jpg|\.gif|\.jpeg/i;
var filename = upload_field.value;
if (filename.search(re_text) == -1) {
alert("File should be either jpg or gif or jpeg");
upload_field.form.reset();
return false;
}
document.getElementById('picture_preview').innerHTML = '<div><img src="ajax-loader.gif" border="0" /></div>';`
upload_field.form.action = 'upload-picture.php';
upload_field.form.target = 'upload_iframe';
upload_field.form.submit();
upload_field.form.action = '';
upload_field.form.target = '';
return true;
}
upload.php
<?php
?>
I want $row['sfname'] in index.html to be accessed along with $_FILE variable here but how can I get the script to store the image in given folder so that I can store the path in the respective user record.
I have tested the upload.php by uploading the files successfully to the file system, now I want the file path to be stored in the sql table. For that I need the user first name the same I get it from the same
My question is; How do I access the input tag ID using the above ajax code to upload.php?
Why not simply put $row['sfname'] as a value of a hidden field as your JavaScript is submitting the whole form?
<input type='hidden' name='sfname' value='" . $row['sfname'] . "'>

File Upload php syntax error

I'm trying to make this code work again. I did something that made in not echo even the file name I'm trying to access. Please help, I've been looking at this for over an hour already with no avail :(
Note: I can't even echo out the $name in here
<?php
error_reporting(0);
if($_POST['submit']){
//file uploading
$name = basename($_FILES['upload']['name']); //name plus extention
print_r($_FILES['upload']);
$t_name = $_FILES['upload']['tmp_name'];
$dir = 'gallery_i';
$thumbs = 'gallery_t';
if(move_uploaded_file($t_name,$dir.'/'.$name)){
$resizeObj = new resize($dir.'/'.$name);//Resize image (options: exact, portrait, landscape, auto, crop)
$resizeObj -> resizeImage(200, 200, 'auto'); //Save image
$resizeObj -> saveImage($thumbs.'/'.$name, 100);
echo 'file upload nicely';
//file upload successfull
}
}
?>
<div class='home'>
<form method='post' action='other.php'>
Name: <input class='reg' type='text' name='prodname'/><br/><br/>
Description:<br/> <textarea class='reg' name='description' rows="4" cols="40"> </textarea> <br/><br/>
<input type='file' name='upload' />
<input type='submit' value='submit' name='submit'/>
</form>
</div>
You need to use mutlipart form data to upload files:
set the form attribute: enctype="multipart/form-data"
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
simple tutorial here: http://www.w3schools.com/php/php_file_upload.asp
The echo only executes if move_uploaded_file($t_name,$dir.'/'.$name) returns true. In your case, it is returning false.
You need to determine if
the file exists (file_exists($t_name))
is readable (is_readable($t_name))
the target directory is writable (is_writable($dir))
no file with the same name already exists (!is_file($dir . '/' . $name))
Only when those conditions are true will move_uploaded_file complete successfully.
As noted by benedict_w, you also need to modify your HTML form to add the attribute enctype with a value of multipart/form-data.
Turn on php's error reporting/display options. Running with those off while develping/debugging is a lot like running a marathon on the side of a cliff at midnight while wearing a blindfold.
In your php.ini:
display_errors: 1
error_reporting: E_ALL
As well, your script will never work. For file uploads to even have a chance of succeeding, you need to specify
<form method="post" action="other.php" enctype="multipart/form-data">
^^^^^^^^^^^^^^^^^^^^^^^^^^^^--- missing
and you're assuming the upload has succeeded. At bare minimum you should have:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($_FILES['upload']['error'] === UPLOAD_ERR_OK) {
.... your file handling code
} else {
die("Upload failed with error code " . $_FILES['upload']['error']);
}
}

Image upload form in PHP does nothing, no error

I have the following PHP code that I am using to upload an image to MySQL. When I click submit nothing happens.
include ("connect.php");
session_start();
$login = $_SESSION['wname'];
if((#$_POST['submit'])&&(isset($_FILES["myfile"]))){
// properties of the uploaded file
$name = $_FILES["myfile"]["name"];
$type = $_FILES["myfile"]["type"];
$size = $_FILES["myfile"]["size"];
$temp = $_FILES["myfile"]["tmp_name"];
$error = $_FILES["myfile"]["error"];
if($name){
die("Error uploading file! Code $error.");
} else {
$place = "avatars/$name";
move_uploaded_file($tmp_name,$place);
$query = mysql_query("UPDATE page SET pid_image_name = '$place' WHERE wname = '$login' ");
die("upload complete <a href='index.php'>View image</a>");
echo "Upload complete!";
}
} else {
die("select a file");
}
Here's my form:
<form action='up.php' method='POST' enctype='multipart/form'>
<input type='file' name='myfile'>
<p> <input type='submit' name='submit' value="upload">
What am I doing wrong?
Another important parameter to check out in php.ini is post_max_size. If you set upload_max_filesize > post_max_size the result is that no data is written to $_POST nor $_FILES for files which sizes exceed post_max_size but are below upload_max_filesize
Format you're code.
Are you submitting the actual HTML form (not the PHP code) as html/multipart?? I am willing to be you're not. (enctype="multipart/form-data" needs added to your form tag!)
Learn to debug - actually put conditions elsewhere and see where your code is failing!
In your HTML for have you got enctype="multipart/form-data"?
Something like this:
<form action='submit.php' method='post' enctype="multipart/form-data">
Edit 1:
if you do this: if(move_uploaded_file($tmp_name, $place)){
echo "did move file<br />";
}else{
echo "move failed<br />";
}
You will get move failed (Or I do with your code)
Edit 2
I found your problem: you misspelt the temp-dir variable: you defined $temp but in the move_uploaded_file you asked for $tmp_name so here the correct code: move_uploaded_file($temp, $place);
Just in case you can't see any errors activate error reporting by setting error_reporting(E_ALL); right after the php tag.

Categories