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'];
}
?>
Related
I'm converting an image to a binary file in IOS, which works just fine. This will be handled by my php script which is suppose to upload this image to my ubuntu server. The problem is i keep getting file=unsuccessful. i've tried different directory paths, but cant seem to solve this issue.
This $directory will return this: /var/www/User/core/ios/
<?
if(!empty($_POST))
{
$message = $_POST['message'];
$directory = $_SERVER['DOCUMENT_ROOT'] . '/User/core/ios/';
$file = basename($_FILES['userfle']['upload']);
$uploadfile = $directory . $file;
var_dump($_FILES);
$randomPhotoID = md5(rand() * time());
echo 'file='.$file;
echo $file;
if (move_uploaded_file($_FILES['userfle']['tmp_name'], $uploadfile)) {
echo 'successful';
}
else
{
echo 'unsuccessful';
}
}
else
{
echo('Empty post data');
}
?>
Check the error file of your php(you can make sure if you enabled the error log in php.ini),
if you don't have the permission or for some other reasons it can't move the file ,there will be a record in that file.
Some time you can try the command setenforce 0 if you confirm you(I means the user of apache) have the permission to move the file but it not work.
By the way if the file you want to move is not upload by post, there is no error log and the move function will return false.
I have a php page that is supposed to store the uploaded image to my server. When I run this, I get the "Upload successful" message, but the picture has not been uploaded.
What could it be?
update: can people please leave a comment as to why they down vote my question. I'm new here and I dont know why this question got down voted. thanks
<?
if(!empty($_FILES['uploaded_file'])) {
if ($_FILES['uploaded_file']['error'] > 0 )
echo "Error: " . $_FILES['uploaded_file']['error'] . "<br />";
else{
// Add the original filename to target path.
$target_path = 'MemberPics\\user'.$userid.'.jpg' ;
$success = move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $target_path);
if(!$success) {
echo "There was an error uploading the file, please try again!";
}else {
echo "Upload successful, please go back to your home page";
}
}
}
?>
I believe the problem you are running into is that you are saving the image in an incorrect location (An invalid one from the looks of your link syntax).
Either of these should work:
$target_path = 'MemberPics/user'.$userid.'.jpg' ;
or
move_uploaded_file($_FILES["uploaded_file"]["tmp_name"], "MemberPics/user" . $_FILES["uploaded_file"]["name"]);
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 have written a form which has 3 text inputs and one file input.
This form posts to a PHP script, which saves the file, as well as performing some other business logic. The program worked well in my localhost, and even on my server.
The program is now embedded in a Facebook application, where it still works- but the uploaded files were not saving, initally; So I changed the uploads directory permissions to 777 (a broad stroke, I know). Now the uploaded files are being saved as a string of numbers "13474022561" with no extension.
Can anyone tell me why? Or how to fix this?
snippet of source code below:
if($_FILES['resume']['type']!='')
{
$target_path = "resumes/";
$target_path = $target_path .time().basename( $_FILES['resume']['name']!="");
if($_FILES['resume']['type']=="application/vnd.openxmlformats-officedocument.wordprocessingml.document" || $_FILES['resume']['type']=="application/pdf")
{
if(move_uploaded_file($_FILES['resume']['tmp_name'], $target_path))
{
echo "success";
}
else
echo "failure";
}
else
{
echo "<center>Resume must be MS DOC or PDF <br/>";
exit;
}
}
This line of code:
$target_path = $target_path .time().basename( $_FILES['resume']['name']!="");
Should be:
$target_path = $target_path . time() . basename( $_FILES['resume']['name']);
Your current code is evaluated like this:
$target_path = 'resumes/' . time() . basename(true);
Which is probably not what you want.
I can't seem to get the following code to work?
$playerfileLocation = "../wp-content/gallery/playerphotos/' . $playerid . '.jpg";
if(file_exits($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
The "playerid" field is a number that gets passed through.
I just can't seem to get it working. Urgh!
You have mismatch of quotes. Try this code.
$playerfileLocation = "../wp-content/gallery/playerphotos/" . $playerid . ".jpg";
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
Update:
Actually I would suggest using below code as whenever PHP see double-quotes, it tries to parse anything in between it, which is not required in this case. This is a small optimization in performance.
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
if(file_exists($playerfileLocation))
echo "The file File exists";
else
echo "The file File does not exist";
Also, to just check if file exists and if not display default image use the following code:
$playerfileLocation = '../wp-content/gallery/playerphotos/' . $playerid . '.jpg';
$defaultfileLocation = '../wp-content/gallery/playerphotos/default.jpg';
if (!file_exists($playerfileLocation)) {
$playerfileLocation = $defaultfileLocation;
}
Your code is misspelled: (exist not exits)
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(file_exits($playerfileLocation))
{
}
else
{
$playerfileLocation=$default_player;
}
In php, we can get variable value in double quotes also. It's good practice to use double quotes in path, so no much indentation is required.
$playerfileLocation = "../wp-content/gallery/playerphotos/$playerid.jpg";
$default_player = "../wp-content/gallery/playerphotos/default.jpg";
if(!file_exists($playerfileLocation))
{
$playerfileLocation=$default_player;
}