Upload php script problems - php

Using Expression Web, want to upload images to my web pages, not getting error messages even though written in script. Set the upload folder to share with everyone. php.ini is set to file_uploads=on;upload_max_size=128M. On firefox the script times out and on IE "Internet Explorer can not display page."
I have been searching for answers for weeks. Every other script I have tried works even though I am a novice at php. This upload thing has gotten me scratching my head. Most recent upload script attempt:
if (isset($_POST['submitted'])) {
// Check for an uploaded file:
if (isset($_FILES['upload'])) {
// Validate the type. Should be JPEG or PNG.
$allowed = array ('image/pjpeg', 'image/jpeg', 'image/JPG', 'image/X-PNG', 'image/PNG', 'image/png', 'image/x-png');
if (in_array($_FILES['upload']['type'], $allowed)) {
$target_path = "C:/uploads/";
$name=$_FILES['upload']['name'];
$error=$_FILES['upload']['error'];
$tmp_name=$_FILES['upload']['tmp_name'];
if($error==UPLOAD_ERR_OK){
if($_FILES['upload']['size']>0){
move_uploaded_file($tmp_name, $target_path.$name);
print ("The file ".$name." has been uploaded.\n");
} else{
print ("There was an error uploading the file, please try again!\n");
}
}elseif ($error==UPLOAD_ERR_NO_FILE) {
print("No files specified.\n");
}else{
print("Upload failed.\n");
}
print("\n");
}//End of !in_array IF.
} // End of isset($_FILES['upload']) IF.
} // End of the submitted conditional.
?>
<div>
<form action="photogallery.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288" />
<p><b>File:</b><input name="upload" type="file" /></p>
<input name="submit" type="submit" value="Submit" />
<input name="submitted" type="hidden" value="TRUE" />
</form>
</div>
I am trying to upload images into upload folder in my directory and then display on my web page. I have put images into the folder manually and they will show on my webpage via a link but it wont work using these scripts. My site is not live it is in developement and I am using XAMPP and Microsoft development Server.

The actual error is hard to tell. But there are a few points to mention on your script (typical upload script problems):
The ['name'] constanct could be empty, which would explain an upload error.
But it could also be tampered with, and contain things like "../Windows/System.ini", which wouldn't be so good for your script since you use it unfiltered as output filename.
The MIME ['type'] is likewise unreliable. But that's likely not the problem here. At the very least use strtolower() on it instead of listing multiple MIME alternatives.
Lastly enable error_reporting(E_ALL); which will tell you if move_upload_file() permission issues are the problem.
I would suggest that you start with a simple
move_uploaded_file($_FILES["upload"]["tmp_name"], "C:/uploads/test.1");
And then gradually extend it with your other conditions to find the cause of your upload issue.

Related

Empty FILES array for large (ish) file uploads

I've got some legacy code that for the life of me I cant see why it's suddenly stopped working.
Very basic upload script:
if($_FILES['csvfile']['name']){
//if no errors...
if(!$_FILES['csvfile']['error']){
//now is the time to modify the future fle name and validate the file
$new_file_name = strtolower($_FILES['csvfile']['tmp_name']); //rename file
//move it to where we want it to be
move_uploaded_file($_FILES['csvfile']['tmp_name'], 'active_leads1.csv');
//echo 'Congratulations! Your file was accepted.';
}else{
//set that to be the returned message
//echo 'Ooops! Your upload triggered the following error: '.$_FILES['csvfile']['error'];
die("Unfortuanatly there was an error: {$_FILES['csvfile']['error']}");
}
}else{
die("Unfortuanatly there was an error: ".print_r($_FILES,true)."");
}
And equally the form is just as basic:
<form action="csvconvert.php" method="post" enctype="multipart/form-data">
<input type="file" name="csvfile" size="25" />
<input type="submit" name="submit" value="Submit" />
</form>
And yet, it fails on a file c. 11MB. Smaller files are ok.
Checked the post limit and upload limit, both are fine (256M and 128M), max input time is 240, and yet if I print_r $_POST and $_FILES they are both empty.
Has anyone come across this before? Any help would be appreciated!
If smaller files are ok, are you sure your upload_max_filesize is larger than 11MB? What is the value of upload_max_filesize when you use echo phpinfo();.

How to Upload a File within HTML Context

I'm trying to get a basic PHP script up and running where a user uploads a file to the server, and the file is saved to a folder on the server. I am using a HTML form that looks like
<form action="upload_file.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50000000" />
<br />
<input type="submit" value="Upload" />
</form>
and I have a PHP file saved as upload_file.php that looks like
<?php
$target = "upload/";
$target = $target . basename($_FILES['uploaded']['name']);
$ok = 1;
if (move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) . "
has been uploaded";
} else {
echo "Sorry, there was a problem uploading your file.";
}
?>
The issue is that after the file is uploaded, it simply shows the php code. Nothing else occurs, and no image is saved in uploads. I've looked at several tutorials, and none of them seem to mention this. I'm guessing it's glaringly obvious, but I could use some help. The error echo "Sorry... never occurs either.
Thanks.
Quick Note: I'm using Apache to host a local web server.
If it simply shows the php code, then PHP is not running. Verify tha PHP is installed and mod_php5 activated.
http://www.thesitewizard.com/php/install-php-5-apache-windows.shtml

Can't upload specific files with PHP

I have recently came across a problem which I've never ever experienced!
I've basically created a form like this:
<form method="POST" enctype="multipart/form-data" action="">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
After that I added a really simple PHP script, just for testing:
<?php
if (!empty($_POST['submit'])) {
echo 'OK';
} else {echo 'empty';}
?>
My settings in WAMP is that the file cannot be more than 2MB. Alright, so now if I upload an image or music or .exe or whatever file, it does shows the "OK" text. Even if it exceeds the file limit.
However when I upload a .zip file which exceeds the limit, it actually shows the "empty" message to me, so basically the form didn't submit, but the page reloaded.
So can anyone please tell me what is going on here? Because I don't really know what could be the issue here.
I don't know how, but I've found the answer.
The POST maximum size is actually more than 8MB. My zip files were much bigger and my music or exe files were not that big.
Thanks everyone for your help.

bool(false) Uploading a file

I have had problems with a simple php script in which I can upload a file to a certain folder. I have tried multiple ways in doing this and I still have not had success.
Any errors in my code or advice on how to correct the issue will be taken gracefully.
Main Php Code:
<p>Browse For a File on your computer to upload it!</p>
<form enctype="multipart/form-data" action="upload_photos.php" method="POST">
Choose Photo:
<input name="userfile" type="file" /><br />
<input type="submit" value="Upload Photo" />
<?PHP
if ($userfile_size>250000)
{$msg=$msg."Your uploaded file size is more than 250KB so please reduce the file size and then upload.<BR>";
$file_upload="false";}
else{
if (!($userfile_type<>"image/jpeg" OR $userfile_type<>"image/tiff" OR $userfile_type<>"image/png"))
{$msg=$msg."Your uploaded file must be of JPG, PNG, or tiff. Other file types are not allowed<BR>";
$file_upload="false";}
}
?>
</form>
</label>
</form>
Php code that is called upon on click (upload_photos.php)
<?php
$target_path="uploads/";
chmod("uploads/", 0755);
$target_path=$target_path . basename( $_FILES['uploadedfile']['name']);
$test=move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path);
if($test) {
echo "The file ". basename( $_FILES['uploadedfile']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
var_dump($test);
}
?>
I do not understand why my end results [upon clicking "Upload Files" Button] include only the following results:
"There was an error uploading the file, please try again!bool(false)"
One more thing: I have also tried using the full computer folder path for $target_path and chmod.
Does anybody see what I am doing wrong?
You have <input name="userfile" but then use $_FILES['uploadedfile'] in your script - use one or the other.
Other than that, make sure the chmod worked and the folder is writable.
bool(false) is the output of var_dump($test);, indicating that move_uploaded_file is returning false.
As a basic debugging step, you should try var_dump($_FILES) to make sure you're accessing the right element of that array (I can tell from your code that you aren't, the index will be the name attribute of your <input type="file"/> element).
You have at least one other serious flaw in your logic... The PHP code in your upload form doesn't make any sense. That block of PHP code will execute server-side before the user has ever uploaded a file. It can't possibly work. The two variables you're checking, $userfile_size and $userfile_type, are not defined anywhere.
In my case, I forgot to create a folder where I want to upload. So check once the specified upload path is available or not.

Calling jrxml file from php for pdf generation

i have to generate jasper reports in my php website.
I want to know what all are needed to make this work. I was told that i needed php-java bridge. But i hope that is for the sole purpose of generating a .jrxml file.
I already have the jrxml file with me.
Now how can i call this file from my php code, for generating jasper report in pdf format ?
In case this file is located on your server you can open it using file_open from any php script.
EDIT: The easiest way to do it seems to be using something like the php-jasper-integration. This way you don't need to use a java-php bridge.
In case you do not have it onyour server but client side only you will need to upload it to your web site and treat this file as you need to using your website script.
Here is some example code to do it:
if(!isset($_FILES['userfile']['tmp_name'])){
// starte Session
//session_start(); // Headers sent out
?>
<form enctype="multipart/form-data" action="myscript.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="500000" />
<!-- Name of input element determines name in $_FILES array -->
Please upload a file.
<br>
<br>
<input name="userfile" type="file" />
<input type="submit" value="UPLOAD" />
</form>
<?php
}
else {
?>
if ($_FILES['userfile']['tmp_name'] == '') die ('No file submitted!');
$target = "uploaded/";
$target = $target . basename( $_FILES['userfile']['name']) ;
$ok=1;
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['userfile']['name']). " has been uploaded. Parsing will start soon. ";
}
else {
echo "Sorry, there was a problem uploading your file.<br>";
}
// now do what you need with your file in $_FILES['userfile']['tmp_name']
Well, i could get what i wanted by using PHP Jasper XML. It is opensource too.
Well jrxml is created with ireports and its executed with java, you need a server like tomcat. look for information of jru-php it will help you

Categories