I'm using PHP to create a website for a FileMaker database. What I want to do is upload an image file so it can be accessed by the database.
This is the code I have (which came from a tutorial):
if (isset($_POST['action']) and $_POST['action'] == 'Upload') {
# set the destination directory for the image
$image_directory = 'http://mydomain.com/Images/My_Example/';
# set the temp and dest file names with paths
$temporary_file = $_FILES['new_image']['tmp_name'];
$destination_file = $image_directory . $_FILES['new_image']['name'];
# move file to the images directory
$result = move_uploaded_file($temporary_file, $destination_file);
if ($result) {
# insert the URL into the product record
$edit = $fm->newEditCommand('Product', $_REQUEST['recid']);
$edit->setField('Thumbnail URL', $destination_file);
$edit->execute();
} else {
$error_message = nl2br("temporary_file = ".$temporary_file."\n");
$error_message.= nl2br("destination_file = ".$destination_file."\n\n");
echo $error_message;
die('There was an error moving the file.');
}
}
I understand that the function 'move_uploaded_file()' will move the temporary file, but I can't see a statement that actually uploads to the temporary file in the first place. What am I missing, please? (I don't know if this is a red herring!)
But 'move_uploaded_file()' fails, and I get the error message (which I added to try to debug it) and the 'die' message. Is there any way to get an error code, to tell me why it failed?
I'm assured that the relevant directory does have read/write access. And 'mydomain.com' is actually replaced by the real domain in the script.
Any advice gratefully received. I'm a newbie to PHP, so please bear that in mind when answering! Many thanks.
Cheers
you are using the destination directory path with 'http://mydomain.com/Images/My_Example/', which will not work.
in order to save the file, you need to give it absolute path on the server - means if the DOCUMENT_ROOT folder is "/var/www/html/", then you'll need to upload it to "/var/www/html/Images/My_Example'.
you can check your DOCUMENT_ROOT path in the $_SERVER['DOCUMENT_ROOT'] variable, with phpinfo() command in php, or in your apache.conf files.
also you need to make sure your php has writing permission on the folder you trying to save into.
The problem is in this statement :
$image_directory = 'http://mydomain.com/Images/My_Example/';
When you upload file, you cannot give the url in the destination path. You need to use the relative path like:
$image_directory = './Images/My_Example/';
first of all: try these error messages
the error message is in: $_FILES['upfile']['error']
switch ($_FILES['upfile']['error']) {
case UPLOAD_ERR_OK:
break;
case UPLOAD_ERR_NO_FILE:
throw new RuntimeException('No file sent.');
case UPLOAD_ERR_INI_SIZE:
case UPLOAD_ERR_FORM_SIZE:
throw new RuntimeException('Exceeded filesize limit.');
default:
throw new RuntimeException('Unknown errors.');
}
secondly: http://mydomain.com/Images/My_Example/ is not a vaild upload path.
http://mydomain.com/Images/My_Example/ is not valid becuase it is a URL path. You need to specify a directory path:
//$image_directory = 'http://mydomain.com/Images/My_Example/';
// change to:
$image_directory = 'path/to/Images/folder/';
Related
I am having a problem with move_uploaded_file().
I am trying to upload a image path to a database, which is working perfectly and everything is being uploaded and stored into the database correctly.
However, for some reason the move_uploaded_file is not working at all, it does not produce the file in the directory where I want it to, in fact it doesn't produce any file at all.
The file uploaded in the form has a name of leftfileToUpload and this is the current code I am using.
$filetemp = $_FILES['leftfileToUpload']['tmp_name'];
$filename = $_FILES['leftfileToUpload']['name'];
$filetype = $_FILES['leftfileToUpload']['type'];
$filepath = "business-ads/".$filename;
This is the code for moving the uploaded file.
move_uploaded_file($filetemp, $filepath);
Thanks in advance
Try this
$target_dir = "business-ads/";
$filepath = $target_dir . basename($_FILES["leftfileToUpload"]["name"]);
move_uploaded_file($_FILES["leftfileToUpload"]["tmp_name"], $filepath)
Reference - click here
Try using the real path to the directory you wish to upload to.
For instance "/var/www/html/website/business-ads/".$filename
Also make sure the web server has write access to the folder.
You need to check following details :
1) Check your directory "business-ads" exist or not.
2) Check your directory "business-ads" has permission to write files.
You need to give permission to write in that folder.
make sure that your given path is correct in respect to your current file path.
you may use.
if (is_dir("business-ads"))
{
move_uploaded_file($filetemp, $filepath);
} else {
die('directory not found.');
}
I am using the following php file upload class to upload my files in relative security. I'll be adding more security features later:
http://www.verot.net/php_class_upload.htm
My problem is though, I would like to upload my file to the folder www.mysite.com/upload which I've already pre-created ready to take the files.
The only problem is I don't know how to set it properly to do that. I've confirmed the form is submitting fine. Here's what I tried to use:
$handle = new class_upload($_FILES['image_upload']);
if ($handle->uploaded) {
$handle->file_new_name_body = 'image_resized';
$handle->image_resize = true;
$handle->image_x = 100;
$handle->image_ratio_y = true;
$handle->process('/home/user/upload');
if ($handle->processed) {
echo 'image resized';
$handle->clean();
} else {
echo 'error : ' . $handle->error;
}
}
I get the error:
"Destination directory can't be created. Can't carry on a process."
What do I set for $handle->process('/home/user/upload'); so that it will upload into the correct directory?
The error message suggests that the user your webserver is running under may not have permissions to create directories... so it would imply the directory you've created is not being targeted.
You'll need full path to this folder... one way to get it would be to use:
$handle->process($_SERVER['DOCUMENT_ROOT'] . '/upload/');
I'd try and avoid this though as the $_SERVER variable can be tampered with or can be inaccurate in general.
The better approach imho would be:
$handle->process(realpath(dirname(__FILE__) . '/upload/');
And ensure the folder you've created has writable permissions for the web-server's user/group.
I suspect, what you are looking for is $handle->process(dirname(__FILE__).'/home');, assuming the calling script is directly in the web root folder.
I want to know if it is possible to save a file to a different folder once it is opened. My current logic is -
$myfile = $_POST['file']; // gets path of (in this case an image)
$size = getimagesize($myfile); //for some reason I get an error message when this fails
// I'm assuming there is a better way of determining if the file is an image file or not.
if($size)
{
//save file to said file path
}
Try:
if($size)
{
copy($myfile, $newfile); //$newfile - with full path!
}
I have searched far and wide on this one, but haven't really found a solution.
Got a client that wants music on their site (yea yea, I know..). The flash player grabs the single file called song.mp3 and plays it.
Well, I am trying to get functionality as to be able to have the client upload their own new song if they ever want to change it.
So basically, the script needs to allow them to upload the file, THEN overwrite the old file with the new one. Basically, making sure the filename of song.mp3 stays intact.
I am thinking I will need to use PHP to
1) upload the file
2) delete the original song.mp3
3) rename the new file upload to song.mp3
Does that seem right? Or is there a simpler way of doing this? Thanks in advance!
EDIT: I impimented UPLOADIFY and am able to use
'onAllComplete' : function(event,data) {
alert(data.filesUploaded + ' files uploaded successfully!');
}
I am just not sure how to point THAT to a PHP file....
'onAllComplete' : function() {
'aphpfile.php'
}
???? lol
a standard form will suffice for the upload just remember to include the mime in the form. then you can use $_FILES[''] to reference the file.
then you can check for the filename provided and see if it exists in the file system using file_exists() check for the file name OR if you don't need to keep the old file, you can use perform the file move and overwrite the old one with the new from the temporary directory
<?PHP
// this assumes that the upload form calls the form file field "myupload"
$name = $_FILES['myupload']['name'];
$type = $_FILES['myupload']['type'];
$size = $_FILES['myupload']['size'];
$tmp = $_FILES['myupload']['tmp_name'];
$error = $_FILES['myupload']['error'];
$savepath = '/yourserverpath/';
$filelocation = $svaepath.$name.".".$type;
// This won't upload if there was an error or if the file exists, hence the check
if (!file_exists($filelocation) && $error == 0) {
// echo "The file $filename exists";
// This will overwrite even if the file exists
move_uploaded_file($tmp, $filelocation);
}
// OR just leave out the "file_exists()" and check for the error,
// an if statement either way
?>
try this piece of code for upload and replace file
if(file_exists($newfilename)){
unlink($newfilename);
}
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $newfilename);
I am uploading a file to soundcloud.com from my own server with using an API in PHP:
if (isset($mime)) {
$tmp_file = $tmp_path . $_FILES['file']['name'];
// Store the track temporary.
if (move_uploaded_file($_FILES['file']['tmp_name'], $tmp_file)) {
$post_data = array(
'track[title]' => stripslashes($_POST['title']),
'track[asset_data]' => realpath($tmp_file),
'track[sharing]' => 'private'
);
if ($response = $soundcloud->upload_track($post_data, $mime)) {
$response = new SimpleXMLElement($response);
$response = get_object_vars($response);
$message = 'Success! Your track has been uploaded!';
// Delete the temporary file.
unlink(realpath($tmp_file));
} else {
$message = 'Something went wrong while talking to SoundCloud, please try again.';
}
} else {
$message = 'Couldn\'t move file, make sure the temporary path is writable by the server.';
}
} else {
$message = 'SoundCloud support .mp3, .aiff, .wav, .flac, .aac, and .ogg files. Please select a different file.';
}
}
This is my code. The temp path is http://122.166.23.38/dev3/bids4less/funkeymusic/upload
and it has permissions 777 (-rwxrwxrwx).
But it shows:
Couldn't move file, make sure the temporary path is writable by the server.
How do I fix this problem?
I believe your temp path in php should point to a directory on your server, not a http location.
This question is too old, but as it were popped anyway...
You don't have to move anything, nor unlink.
Just read uploaded file from it's temporary location.
And it comes extremely handy to split your complex task into smaller chunks, in order to locate an error. Try to send a static file first, then test file upload, and only then join these tasks into final application.
TRY S_SERVER you will get brief description from this url http://php.net/manual/en/reserved.variables.server.php
Here is the example given to find address for your server
try big example to sort out you method
What's the file size, and how long takes it to upload the file?
Can you please verify those 2 php.ini values:
max_execution_time
post_max_size
Maybe PHP's upload facility is constrained in some way? Is PHP in safe_mode?