I am getting the following errors:
Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 5
Notice: Undefined index: theimage in C:\wamp\www_upload\index.php on line 7
Here is the code:
<?php
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']). " has been uploaded</p>";
} else{
echo "<p>There was an error uploading the image, please try again!</p>";
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title></title>
<meta name="" content="">
</head>
<body>
<form enctype="multipart/form-data" action="index.php" method="POST">
<fieldset>
<input name="theimage" type="file" />
<input type="submit" value="Upload" />
</fieldset>
</form>
</body>
</html>
Any ideas on how to fix this please ?
<?php
if(!empty($_FILES['theimage'])) {
// YOUR CURRENT PHP CODE HERE
?>
The problem is the upload script is executing even if nothing is posted (like when you first load the page).
Check to make sure you're getting data in $_FILES or $_POST before you try to do anything with them:
<?php
if(isset($_FILES)) { // if(isset($_POST))
// would work as well
$target_path = "images/";
$target_path = $target_path . basename( $_FILES['theimage']['name']);
if(move_uploaded_file($_FILES['theimage']['tmp_name'], $target_path)) {
echo "<p>The image ". basename( $_FILES['theimage']['name']).
" has been uploaded</p>";
} else {
echo "<p>There was an error uploading the image!</p>";
}
}
?>
Change your submit button to:
<input type = 'submit' name = 'theSubmitBtn' value = 'Upload'>
Then, wrap all of your php code at the top inside of this if check:
if(isset($_POST['theSubmitBtn'])) {
... Your Code ...
}
See if that works for you. On a side note, you will also need to add much more error checking if this is to be used in a production environment.
Related
We have a website using PHP 5.2 and it is hosted in Windows Plesk Server. We are now having issue in uploading any files via PHP. When we try to do so we are getting following error.
There was an error uploading the file, please try again!Error Code:6
upload_tmp_dir has local value "C:\Inetpub\vhosts\xxxxxx.xxx\httpdocs\tmp" and master value "C:\Windows\Temp" .
Can anyone suggest what should be the permission of these folders or do we need to check something else to fix this upload issue via PHP?
Here are the scripts I have used to test the Upload.
Script 1
<!DOCTYPE html>
<html>
<head>
<title>Upload your files</title>
</head>
<body>
<form enctype="multipart/form-data" method="POST">
<p>Upload your file</p>
<input type="file" name="uploaded_file"></input><br />
<input type="submit" value="Upload"></input>
</form>
</body>
</html>
<?PHP
if(!empty($_FILES['uploaded_file']))
{
$path = "newupload/";
$path = $path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $path)) {
echo "The file ". basename( $_FILES['uploaded_file']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!Error Code:". $_FILES['uploaded_file']["error"];;
}
}
?>
Script 2
<?php
echo '<form action="" method="post" enctype="multipart/form-data" name="uploader" id="uploader">';
echo '<input type="file" name="file" size="50"><input name="_upl" type="submit" id="_upl" value="Upload"></form>';
if( $_POST['_upl'] == "Upload" ) {
if(#copy($_FILES['file']['tmp_name'], $_FILES['file']['name'])) { echo '<b>Upload SUKSES !!!</b><br><br>'; }
else { echo '<b>Upload GAGAL !!!</b><br><br>'; }
}
?>
Script 1 gave the error "There was an error uploading the file, please try again!Error Code:6" . Script 2 showed upload success. But uploaded file was missing.
This issue might have been discussed multiple times but I wanted a simple PHP script to upload a file, without any separate action file and without any checks. Below is my written code:-
<html>
<head>
<title>PHP Test</title>
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="upload file" name="submit">
</form>
</head>
<body>
<?php echo '<p>FILE UPLOAD</p><br>';
$tgt_dir = "uploads/";
$tgt_file = $tgt_dir.basename($_FILES['fileToUpload']['name']);
echo "<br>TARGET FILE= ".$tgt_file;
//$filename = $_FILES['fileToUpload']['name'];
echo "<br>FILE NAME FROM VARIABLE:- ".$_FILES["fileToUpload"]["name"];
if(isset($_POST['submit']))
{
if(file_exists("uploads/".$_FILES["fileToUpload"]["name"]))
{ echo "<br>file exists, try with another name"; }
else {
echo "<br>STARTING UPLOAD PROCESS<br>";
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $tgt_file))
{ echo "<br>File UPLOADED:- ".$tgt_file; }
else { echo "<br>ERROR WHILE UPLOADING FILE<br>"; }
}
}
?>
</body>
</html>
I saved it in /var/www/html/phps/ location. But everytime I try to upload the file, I get ERROR WHILE UPLOADING FILE error. What am I doing wrong here. P.S. I have no previous experience of PHP, I just started with bits & pieces from internet.
Thanks
kriss
<?php
$target_dir = "uploads/";
$target_file = $target_dir .
basename($_FILES["fileToUpload"]["name"]);
if(move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ".basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
}
else {
echo "Sorry, there was an error uploading your file.";
}
?>
I hope that this thing will work and this is what you are in need of.
<?php
$name = $_POST['name'];
$image = $_FILES['fileToUpload']['name'];
$tempname = $_FILES['fileToUpload']['tmp_name'];
move_uploaded_file($tempname, "foldername/$image");?>
Please i need your help on this. Any time i transfer this file into remote server through ftp, it often get missing on the server somehow. I always have to re-upload it. Recently all i get are errors like this PHP Warning: require(maxUpload.class.php): failed to open stream: No such file or directory.
I know the errors are due to due to my require() function.
Is there any reason this file keep getting lost on the remote server?
<?php
require 'maxUpload.class.php'
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Upload Page</title>
</head>
<body>
<?php
$myUpload = new maxUpload();
//$myUpload->setUploadLocation(getcwd().DIRECTORY_SEPARATOR);
$myUpload->uploadFile();
?>
</body>
This is maxUpload.class.php
<?php
class maxUpload{
var $uploadLocation;
function maxUpload(){
$this->uploadLocation = getcwd().DIRECTORY_SEPARATOR."/images". DIRECTORY_SEPARATOR;
}
function setUploadLocation($dir){
$this->uploadLocation = $dir;
}
function showUploadForm($msg='',$error=''){
?>
<div id="container">
<div id="content">
<?php
if ($msg != ''){
echo '<p class="msg">'.$msg.'</p>';
} else if ($error != ''){
echo '<p class="emsg">'.$error.'</p>';
}
?>
<form action="" method="post" enctype="multipart/form-data" >
<center>
<label>Upload Image (Jpeg Only)
<input name="myfile" type="file" size="30" />
</label>
<label>
<input type="submit" name="submitBtn" class="sbtn" value="Upload" />
</label>
</center>
</form>
</div>
</div>
<?php
}
function uploadFile(){
if (!isset($_POST['submitBtn'])){
$this->showUploadForm();
} else {
$msg = '';
$error = '';
if (!file_exists($this->uploadLocation)){
$error = "The target directory doesn't exists!";
} else if (!is_writeable($this->uploadLocation)) {
$error = "The target directory is not writeable!";
} else {
$target_path = $this->uploadLocation . basename( $_FILES['myfile']['name']);
if(#move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) {
$msg = basename( $_FILES['myfile']['name']).
" <b style='color:white;'>was uploaded successfully!</b>";
} else{
$error = "The upload process failed!";
}
}
$this->showUploadForm($msg,$error);
}
}
}
?>
Your file is not missing, this error also indicate that you are giving wrong path to the file, make sure maxUpload.class.php is in same folder. you need to give correct path to require() function where to locate this file.
You can also use getcwd() to Gets the current working directory.
something like echo getcwd();. you can use chdir() function to go to specific folder by giving correct path chdir("../");
i don't know your folder structure but add
require '../maxUpload.class.php' or
require '/maxUpload.class.php'
For my project i am using PHP and MySql. In that i was tried to upload an image to the mysql database. In that i faced one terrible error. My html code was like this
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Learing new things</title>
<style>
body
{
margin:4%;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" method=post name=imaging action="upload.php">
<input type="file" name=file><input type="submit" name=upload value=Upload>
</form>
</body>
</html>
"file" was the name of my file upload field. PHP code like this
<?php
$file=$_FILES['file'];
var_dump($file);
if(isset($_FILES['file']['name']))
{
echo "Image Uploaded";
echo $file['name'];
}
else
echo "Image not Uploaded";
?>
Whatever happen whether the file is uploaded or not uploaded, in my PHP page it is always executing the echo "image upload". i tried without selecting a file and clicked the upload still i am getting the same. How do i find whether a file is selected and uploaded in the html page. why i am getting the same message. it is not executing the else block.
Format html code
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Learing new things</title>
<style>
body
{
margin:4%;
}
</style>
</head>
<body>
<form enctype="multipart/form-data" method="post" name="imaging" action="upload.php">
<input type="file" name="file" /><input type="submit" name="upload" value="Upload" />
</form>"
</body>
</html>
Php check if file was selected
if (empty($_FILES['file']['name'])) {
// No file was selected for upload
}
Modify your Php script to this.
<?php
$target_dir = "(your target directory)/";
$target_file = $target_dir .basename($_FILES["uploadfile"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["upload"])) {
$check = getimagesize($_FILES["uploadfile"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
?>
I have a server apache and mysql at my friend's house, i can connect to both of them, but i want to upload pictures from my Macbook to my directory in the server (Ubuntu), i have some issues and tried many things but any of them worked, please help me thanks :)
The html form:
<html>
<head>
</head>
<body>
<form accept-charset="UTF-8" action="php.php" method="POST" enctype="multipart/form-data">
<input type="file" name="img"/>
<input type="submit"/>
</form>
</body>
</html>
The php :
<html>
<head> <meta charset="UTF-8"/> </head>
</html>
<?php
$bdd = new PDO('mysql:host=***.***.***.***;port:****;dbname=**********', '*******', '**********',array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
echo $_FILES['img']['name'];
$target1 = "/home/knight/";
$target = $target1 . basename( $_FILES['img']['name']);
if(move_uploaded_file($_FILES['img']['tmp_name'], $target))
{
//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " 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 file.";
}
?>