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.";
}
?>
Related
This code working fine in localhost but working in online webserver
whenever I try to upload the files using online hosting, this code does not work for me:
<?php
if (isset($_POST['submit'])) {
$name = $_FILES['file']['name'];
$temp_name = $_FILES['file']['tmp_name'];
if (isset($name)) {
if (!empty($name)) {
$location = 'images/';
if (move_uploaded_file($temp_name, $location . $name)) {
echo 'File uploaded successfully';
}
}
} else {
echo 'You should select a file to upload !!';
}
}
?>
Here is the html code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form action="save.php" method="POST" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" name="submit">
</form>
</body>
</html>
What am I doing wrong?
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.
I followed every tutorial in the internet that I find to upload a file. But still, It failed. It gave me this error:
Warning: move_uploaded_file(/var/www/projects/upload/TASK.txt): failed to open stream: No such file or directory in /var/www/projects/test/upload.php on line 6 Warning: move_uploaded_file(): Unable to move '/tmp/phpjr2JJA' to '/var/www/projects/upload/TASK.txt' in /var/www/projects/test/upload.php on line 6 Something went wrong
index.html
<head>
<title></title>
</head>
<body>
<form method="POST" action="upload.php" enctype="multipart/form-data">
<input type="file" name="upload" ><br />
<input type="hidden" name="MAX_FILE_SIZE" value="1024" />
<input type="submit" name="submit" value="Submit" />
</form>
</body>
</html>
upload.php
<?php
$target_path = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
Can you help me and point out where I went wrong? I'm using ubuntu 12.04 and also I tried to change the permission for the /upload folder to 755 and checked the file_upload in php.ini is ON
Any help would be much appreciated. Thanks!
$_SERVER['DOCUMENT_ROOT'] gives /var/www/projects/upload.. as output
/ in the start is cause of error
Hence
try with relative path
<?php
$target_path = "upload/";
$target_path = $target_path . basename( $_FILES['upload']['name'] );
if ( move_uploaded_file($_FILES['upload']['tmp_name'], $target_path) ) {
echo "has been uploaded";
} else {
echo "Something went wrong";
}
?>
worked for me
File & Dir permissions...
Add in your FTP Program 666 to the files and dirs what you need to write.
I think ur file upload location incorrect
index.php
<html>
<head>
<title>Upload your file</title>
</head>
<body>
<form action="upload.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="Upload" value="Submit">
</form>
</body>
upload.php
<?php
$target="give the path you want to store the file/";
if ($_FILES["file"]["error"] > 0)
{
echo "Error: " . $_FILES["file"]["error"] . "<br>";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
$target. $_FILES["file"]["name"]);
}
?>
I'm trying to test a simple file upload script but it fails. Here is the html portion.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title="testing"></title>
</head>
<body>
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input type="file" name="uploaded"/>
<input type="submit" value="Upload File"/>
</form>
</body>
And below this is the php portion.
<?php
$target = "upload/";
$target = $target . basename( $_FILES['uploaded']['name']) ;
$ok=1;
if ($uploaded_size > 350000)
{
echo "Your file is too large.<br>";
$ok=0;
}
if ($uploaded_type =="text/php")
{
echo "No PHP files<br>";
$ok=0;
}
if ($ok==0)
{
Echo "Sorry your file was not uploaded";
}
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.";
}
}
?>
It fails at this line:
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
And execute the else portion: "Sorry, there was a problem uploading your file." How can I get it to work without failing. Thank you for you help.
The most likely reason is lack of write access to the target directory.
Also, I'd check if $target resolves correctly, as there could be artifacts in the filename that seem to be passed directly to the script and are not filtered.
You could test the permissions with
is_writable( PATH_OF_FOLDER )
I also tend to test the existance of the file with
is_file( PATH_OF_NEW_FILE )
before confirming the upload is successful!
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.