Uploading a file using PHP - php

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?

Related

Renaming Server File Error (http wrapper does not support renaming) [PHP]

On my page users can upload documents, which will be saved in a user specific folder. the directories are stored in a url: http://localhost/folder/user/documentA_user_timestamp.ext inside a database.
Users can also delete a file, which deletes the file's entry inside the database but i want the file to be moved to a specified archive folder. However i always get this error message when the php rename() tries to do its work:
http wrapper does not support renaming in...
I can't seem to get my head around the error message and figure out whats the cause for it
$filepath = $_POST['file'];
$archivePath = FILESYS_DOCS_ARCHIVE . basename($filepath);
if (!file_exists(FILESYS_DOCS_ARCHIVE)){
mkdir(FILESYS_DOCS_ARCHIVE, 0777);
}
$success = rename($filepath, $archivePath);
if ($success){
echo "SUCCess";
} else {
echo $archivePath;
}
use this
$dir = str_replace('http://','',base_url());
rename($dir.'older filename', $dir.'/new filename' )

php move_uploaded_file not creating file

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.');
}

Setting destination for secure file uploads php

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.

Issues of Parse Email and Save Attachments into directory

I am using this script(http://stuporglue.org/mailreader-php-parse-e-mail-and-save-attachments-php-version-2/) to save email attachment on my server. You can also view the complete script on browser here: http://stuporglue.org/downloads/mailReader.txt
Everything works fine but there are 2 problems here.
1) The file name of the image that i saved into the directory is not an image: 1360341823_test_jpg
How to convert the file name from 1360341823_test_jpg to 1360341823_test.jpg
in the script?
2) The permission of the file that saved in the directory is 600.
How to make it default 755 or 775?
I believe this is the function to convert the image in the script.:
function saveFile($filename,$contents,$mimeType){
global $save_directory,$saved_files,$debug;
$filename = preg_replace('/[^a-zA-Z0-9_-]/','_',$filename);
$unlocked_and_unique = FALSE;
while(!$unlocked_and_unique){
// Find unique
$name = time()."_".$filename;
while(file_exists($save_directory.$name)) {
$name = time()."_".$filename;
}
// Attempt to lock
$outfile = fopen($save_directory.$name,'w');
if(flock($outfile,LOCK_EX)){
$unlocked_and_unique = TRUE;
} else {
flock($outfile,LOCK_UN);
fclose($outfile);
}
}
fwrite($outfile,$contents);
fclose($outfile);
// This is for readability for the return e-mail and in the DB
$saved_files[$name] = Array(
'size' => formatBytes(filesize($save_directory.$name)),
'mime' => $mimeType
);
}
Any help?
The original script used the data to store in the DB but I think you are trying to save it in the file. You are creating the file without extension here:
// Attempt to lock
$outfile = fopen($save_directory.$name,'w');
Either add the .jpg after the line as:
#outfile.=".jpg";
Other way if you don't want to change script then you can get use as:
$contents = file_get_contents($save_directory.$name);
$outfile = fopen($save_directory.$new_name,'w');
write($outfile,$contents);
fclose($outfile);
This would resolve your first problem and for second question kindly use the FTP or Control panel provided to access the files to change the ownership rights. If you don't know about any thing then you contact your Web Hosting Service Provider to share the ownership from 755 to 775

Save Uploaded Text File to Web Server

I have my users uploading a text file which then gets processed by my application. Once the processing is done, I would like to save a copy of this text file somewhere on my server for future reference. Currently, the uploaded text file stays in the PHP temp folder until it is closed by my app.
What's a simple way to accomplish this?
BTW, I'll need to know how to do this on my web server along with localhost (for testing).
You can use the fwrite function (this is probably not a very good idea in this particular example though.
$fp = fopen('data.txt', 'w');
fwrite($fp, $yourContents);
fclose($fp);
But, if you already have the file simply copy it using the copy command (if you want to keep it in the temp folder that is, if not move it with the rename function instead).
To copy, do something like this
$tempfile = 'tempfile.txt';
$newfile = 'newfile.txt';
if (copy($tempfile, $newfile)) {
echo "success!";
} else {
echo "misery :(";
}
To move with rename
// Rename returns a bool, just as in the copy example
rename("/tmp/tempfile.txt", "/home/user/files/newfile.txt");
Added this: To move with move_uploaded_file
Please note, I didn't test this in a development environment. This may not execute perfectly.
$uploads_dir = 'C:\\movefiles\\here\\';
foreach ($_FILES["upload-tracking-file"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["upload-tracking-file"]["tmp_name"][$key];
$name = $_FILES["upload-tracking-file"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir\\$name");
}
}
References
Copy, http://php.net/manual/en/function.copy.php
Move (rename), http://php.net/manual/en/function.rename.php
move_uploaded_file, http://php.net/manual/en/function.move-uploaded-file.php
fwrite, http://php.net/manual/en/function.fwrite.php
use php's function ob_start(), and file_put_contents() this should help you. this links will help you if not post your reply
php.net/manual/en/function.ob-start.php, php.net/manual/en/function.file-put-contents.php

Categories