I get following errors and I don't know why.
Notice: Undefined index: uploaded in C:\xampp\htdocs\site\upload.php on line 3
Notice: Undefined variable: uploaded_size in C:\xampp\htdocs\site\upload.php on line 7
Notice: Undefined variable: uploaded_type in C:\xampp\htdocs\site\upload.php on line 14
Notice: Undefined index: uploaded in C:\xampp\htdocs\site\upload.php on line 29
I tried to include the source of the php in this post but couldn't.
Here is link to pastebin: My source code
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
//This is our size condition
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is our limit file type condition
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
//If everything is ok we try to upload it
else
{
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.";
}
}
?>
Edit 1 : I got the code from about.com about.com
1.) In your $FILES array, there's no uploaded index defined. Possibly because it's $_FILES That's the first one. Try
<pre>
print_r($FILES);
print_r($_FILES);
</pre>
to see what is in there.
2.) ¿Where are you initializing those variables being used in the conditionals? You must extract data from the file element in the array and assign it to the variables before using it.
The first notice is telling you that on this line:
$target = $target . basename( $_FILES['uploaded']['name']) ;
There is no 'uploaded' index into the $_FILES array because no file was uploaded in a form with the file input named uploaded.
The next notice is telling you that on this line:
if ($uploaded_size > 350000)
You are accessing a variable named $uploaded_size which has never previously been defined, so how could it possibly hold some value greater than 350000?
Same issue with the next two notices. You seem to be missing some code, and are testing code for handling uploaded files without uploading a file, or without using a form with the enctype and input names it expects.
If you find that the $_FILES array does not contain any reference to your uploaded file, make sure your form tag includes enctype="multipart/form-data".
Related
I've made a form that I want the user to submit as an entire PDF to be saved on the server. I've been trying to make a php script that handles the files coming in. Basically, I've taken the example from W3 schools and tried to adapt it:
<?php
$file = file_get_contents("php://input");
$target_dir = "uploads/";
$target_file = $target_dir . basename($file);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($file, $target_file)) {
echo "The file ". basename( $file). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
?>
However, it's not working. I get the second error message: "Sorry, there was an error uploading your file."
I'm fairly new to php and would greatly appreciate some help.
Thanks!
UPDATE: All the documentation and examples involve POST data from an HTML form, so the input field name is known in each. My PDFs are generated server side with random names, so I had to adapt the examples. I made this one which worked with an HTML form:
<?php
foreach ($_FILES as $name => $value)
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES[$name]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
move_uploaded_file($_FILES[$name]["tmp_name"], $target_file)
?>
However, when I submitted from the PDF itself using Adobe Acrobat, I got a series of errors:
http://localhost/save.php[22/01/2016 20:23:05]
Notice: Undefined variable: name in C:\xampp\htdocs\save.php on line 4
Notice: Undefined index: in C:\xampp\htdocs\save.php on line 4
Notice: Undefined variable: target_dir in C:\xampp\htdocs\save.php on line 4
Notice: Undefined variable: name in C:\xampp\htdocs\save.php on line 7
Notice: Undefined index: in C:\xampp\htdocs\save.php on line 7
Although my script uploads files from HTML forms without needing to know the input field name (which is always known in the documentation examples), which I was pleased about, it doesn't work when submitted from within the PDF itself.
Does anyone know why that would be?
If you submit a PDF form by sending the whole document, you just have to take the raw post data as the document data. There are no individual post fields but:
$fileContent = file_get_contents("php://input");
...is enough. You have the whole PDF document in $fileContent now. So just save this (after validation!) and you're done.
Ok Basically i am following this guide which is pretty straight forward but, I am at the point now where i am completely lost. I don't want to have to start all over again and I hope it's just a minor adjustment but anyhow heres the error:
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /I don't want to publicize the path name/html/add.php on line 39
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpqd62Gk' to 'thumbnails/' in /I don't want to publicize the path name/add.php on line 39
Sorry, there was a problem uploading your cover. Please check it is the appropriate size and format.
Warning: move_uploaded_file() [function.move-uploaded-file]: The second argument to copy() function cannot be a directory in /I don't want to publicize the path name/add.php on line 52
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpFVlGsv' to 'audio/' in /I don't want to publicize the path name/add.php on line 52
Sorry, there was a problem uploading your song. Please check it is the appropriate size and format.
And here is the "add.php" which is executed after submitting the form :
<?php
include "db_config.php";
//This is the directory where images will be saved
$targetp = "thumbnails/";
$targetp = $targetp . basename( $_FILES['cover']['artist']);
//This is our size condition
if ($uploaded_size > 100000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This is the directory where songs will be saved
$targets = "audio/";
$targets = $targets . basename( $_FILES['song']['artist']);
//This is our size condition
if ($uploaded_size > 6000000)
{
echo "Your file is too large.<br>";
$ok=0;
}
//This gets all the other information from the form
$title=$_POST['title'];
$artist=$_POST['artist'];
$cover=($_FILES['cover']['artist']);
$song=($_FILES['song']['artist']);
$today = date("Ymd");
$ip = $_SERVER['REMOTE_ADDR'];
//Writes the information to the database
mysql_query("INSERT INTO `Thumbnails` VALUES ( '', '$artist - $title', '$cover', '', '$song', '$title', '$artist', '$today', '$ip')") ;
//Writes the photo to the server
if(move_uploaded_file($_FILES['cover']['tmp_name'], $targetp))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['cover']['artist']). " has been uploaded, and your information has been added to the directory";
}
else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your cover. Please check it is the appropriate size and format.";
}
//Duplicate for song
if(move_uploaded_file($_FILES['song']['tmp_name'], $targets))
{
echo "The file ". basename( $_FILES['song']['artist']). " has been uploaded, and your information has been added to the directory";
}
else {
echo "Sorry, there was a problem uploading your song. Please check it is the appropriate size and format.";
}
?>
Perhaps a step in the right direction would be greatly appreciated as it has gotten to the point where it all feels like scribble on a screen but, I'd hate to start all over again.
Try to
Change
basename( $_FILES['cover']['artist']);
To
basename( $_FILES['cover']['name']);
try
$targetp = "/thumbnails/;
$targets = "/audio/";
or specify full path name like c:/some/path/here
I am making an application on my server, where the user uploads an image through some HTML combined with javascript.
The user finds an image on the computer through
<form action="uploadimage.php" method="post"
enctype="multipart/form-data">
<label for="file">Filnavn:</label>
<input type="file" name="file" id="file" value="100000" />
Then the point behind the javascript, is to validate on the users image
if(picture_headline.value == "" || picture_uploaded.value == "" || !ischecked)
{
// Don't execute, stay on same site
}
else
{
// execute php and upload image
}
the php is an upload image php script
<?php
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/";
// add the original filename of our target path
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
// Moves the uploaded file into correct folder
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.";
}
?>
I must say I am a bit confused here, since I have only been working with html, php and javascript for a few days now.
Am I totally off or what?
I found some "simple" examples online, which I put on my server through cuteFTP, but everytime i press upload, the website just sends me to the .php file and says the site doesn't exist.
Like Boann points out you're trying to access a non-existent file in your PHP code ("uploaded" and "uploadedfile" rather than "file" (which is what you named the field in your HTML form)).
But regarding "running PHP from JavaScript": You don't have to. The JavaScript should only return false if the form is invalid. If it's valid you don't need to do anything and the form will submit, in turn running your PHP script:
form.onsubmit = function () {
if (!formIsValid()) {
return false;
}
};
If the form is invalid it won't submit (the return false bit (you could use event.preventDefault() instead)), if it is valid nothing will happen and the form will do what it does (ie submit the data to the server).
Each array key in $_FILES corresponds with the name attribute of a file field in the form, so to match your form it should be 'file' rather than 'uploaded' or 'uploadedfile':
<?php
// The file is being uploaded into the folder "upload"
$target = "/navnesutten.eu/facebook/uploads/";
// add the original filename of our target path
$target = $target . basename( $_FILES['file']['name'] ) ;
$ok=1;
// Moves the uploaded file into correct folder
if(move_uploaded_file($_FILES['file']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['file']['name']). " has been uploaded";
}
else {
echo "Sorry, there was a problem uploading your file.";
}
I'm doing a simple file upload using the following script:
$errors = '';
$target_path = "[PATH HERE]";
$target_path = $target_path . basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
For some reason, I get an error uploading the file and the file type is not displayed. It seems to only grab the name of the file without the extension (i.e. "test" rather than "test.pdf"). I'm sure it's something simple, but what am I doing wrong?
If you check the error element in the files array, you'll probably find it's some value other than 0. The error should be 0 if nothing went wrong. Otherwise, compare the value stored in error against the PHP documentation to determine what went wrong.
Perhaps your entering the path wrong (ending slash), or php dont have permission to write to the directory.
<?php
error_reporting(E_ALL); // Show some errors
$target_path = "/var/www/somesite.com/uploads/"; // Would require a ending slash
$target_path = $target_path.basename($_FILES['uploadFile']['name']);
if(move_uploaded_file($_FILES['uploadFile']['tmp_name'], $target_path)) {
$errors = "The file ". basename( $_FILES['uploadFile']['name']). " has been uploaded";
} else{
$errors = "There was an error uploading the file, please try again! Type: " . $_FILES['uploadFile']['type'];
}
?>
ho everyone i am trying to upload images but i got a warning that i didn't understand
here is the code
// print out contents of $_FILES ARRAY
print "Print out of the array of files: FILES <br>";
print_r($_FILES);
print "<br><br>";
$F1 = $_FILES["fname"];
print_r($F1);
print "<br><br>";
// 0 means a successful transfer
if ($_FILES["fname"]["error"] > 0) {
print "An error occurred while uploading your file";
exit(0);
}
// only accept jpg images pjpeg is for Internet Explorer.. should be jpeg
if (!($_FILES["fname"]["type"] == "image/pjpeg")) {
print "I only accept jpg files!";
exit(0);
}
// divide size by 1024 to get it in KB
if ($_FILES["fname"]["size"] / 1024 > 50) {
print "Your gif file is too large! Less that 50KB please!";
exit(0);
}
// check that file is not already there in your uploads folder
if (file_exists("Uploads/" . $_FILES["fname"]["name"])) {
print "$F1[name] already exists. Choose another name for your file.";
exit(0);
}
// move file from temp location on server to your uploads folder
**move_uploaded_file($_FILES["fname"]["tmp_name"], "Uploads/".$_FILES["fname"]["name"]);**
print "Stored in:"." Uploads/".$_FILES["fname"]["name"];
// save location of upload to text file uploads.txt for later use
$datafile = fopen("uploads.txt","a");
flock($datafile,1);
fwrite($datafile, "Uploads/".$_FILES["fname"]["name"]."\n");
flock($datafile,3);
fclose($datafile);
and the warning is( refer to bold line)
Warning:
move_uploaded_file(Uploads/avatar3.jpg):
failed to open stream: No such file or
directory in
/home/www/mariam.awardspace.info/php/posts.php
on line 57
Warning: move_uploaded_file(): Unable
to move '/tmp/phprqcpQB' to
'Uploads/avatar3.jpg' in
/home/www/mariam.awardspace.info/php/posts.php
on line 57
thanks in advance
2 things come to mind.
Try using a path like
$_SERVER['DOCUMENT_ROOT'].'/path/to/file.jpg');
Then make sure the uploads folder exists in the root folder of your site
The directory "Uploads" does not exist or you don't have sufficient permissions to write to it.