File upload php with file array - php

i try to upload files into my server also to my database.
However, i cannot upload them.
I can see my two files with below code,
echo $_FILES['file']['name'][0];
echo $_FILES['file']['name'][1];
$fileName = $_FILES['file']['name'][0];
$add = "img01.imgsinemalar.com/images/haber_anasayfa/" . $fileName;
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
NCore::db('NEWS')->insertAsArray(array('CONTENT' => $_POST['TvSpotdesc'], 'ADD_DATE' => $date, 'TITLE' => $_POST['newsTitle'],IMAGE_MAIN => $add))->execute();
echo "Haber başarıyla gönderildi.";
} else {
echo "Failed to upload file Contact Site admin to fix the problem";
}else {
echo $msg;
}
But above code does not upload my files, it goes directly to the else statement, why?
Failed to upload file Contact Site admin to fix the problem

Your code should be outputting two notices and a warning from this line:
if (move_uploaded_file($_FILES[file_up][tmp_name], $add)) {
The two notices would be because you have not quoted file_up and tmp_name and the warning to inform you of the reason move_uploaded_file has failed, as documented. You should check out the logs to see these for yourself (development without seeing any error messages is not fun).
The reason it fails is that you cannot move a file to a remote server -- $add is not a local path.

Rename $_FILES[file_up][tmp_name] to $_FILES['file']['tmp_name']
There is no such variable as file_up as per the code you pasted. Also, files can only be stored on the same server. so gove relative path like /home/www/myapp/images/ instead of server name.

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' )

Use of move_uploaded_file() function issue

I have hosted my website to my server.The website is functioning well as same as the localhost.However,When i try to move uploaded files to the uploads folder,the move_uploaded_file() function is not working as it is working with the localhost.On using the mysql_error() function to find the error,it is not giving me any results.What is the problem with my code
code one is not giving me any results
code 1
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo mysql_error();
}
code two is outputing the else phrase
echo'cannot move file to folder';
code 2
if(move_uploaded_file($_FILES['Upfile']['tmp_name'],$location.$_FILES['Upfile']['name'])){
echo'proceed';
}else{
echo 'cannot move file to folder ';
}
Make sure that the second parameter i.e destination where you are storing the uploaded file should be in string format.

How to log errors for move_uploaded_file to a file

I have some code that uploads a file, its the exact same code i had working on another server but its not working on this new server
// Move the file into the new folder
move_uploaded_file($_FILES["file"]["tmp_name"],'./img/myfolder/1/'. $_FILES["file"]["name"]);
I know i can use:
if(move_uploaded_file($_FILES["file"]["tmp_name"],'./img/myfolder/1/'. $_FILES["file"]["name"])){
echo "success";
} else{
echo "failure";
}
However i have 2 problems, the script is not ran directly from the page, another application which i dont have access to source code to calls that page and sends the image. So i cant do any in page errors i need to log the errors to a file.
The 2nd problem and the main issue is i am not sure how to find out what the error is. Is there an error code i can pull if the else statement is called. I have set it to 777 for the folders and subfolders just for testing purposes to rule out permission issues, ill fix that after getting the problem resolved before pushing to production.
Also i checked the apache server error.log file and it shows nothing
Here i would do something like this.
$debug_file = _DIR_.'/debug.txt';
$source = $_FILES["file"]["tmp_name"];
$dest = './img/myfolder/1/'. $_FILES["file"]["name"];
if(! #move_uploaded_file($source,$dest) ){
file_put_contents( $debug_file, "ERROR[ ".date('Y-m-d H:i:s')." ] Could not move[ $source ] to[ $dest ]\n", FILE_APPEND);
exit();
}
Then when you have the source and dest paths you can make sure they actually exist in the right places.
-note- the # sign will suppress the normal PHP warning for failing to move the file, but as we are logging it our self, this just prevents it from getting in the way.
Also I put an exit in, I'm assuming its a requirement of this script to have the file to work properly, and that way it's enough just to fail, no need to check for success.
Most likely, the file path is wrong
Also you could even output buffer the php error as well like this,
ob_start();
$moved = move_uploaded_file($source,$dest);
$message = ob_get_clean();
if(!$moved){
file_put_contents( $debug_file, "ERROR[ ".date('Y-m-d H:i:s')." ] Could not move[ $source ] to[ $dest ] PHP message[ $message ]", FILE_APPEND);
exit();
}
Output buffing works wonders on scripts ran in the background,
http://php.net/manual/en/function.ob-get-clean.php

PHP File Upload Error Line 17

I wrote a script that would allow me to upload images and files to my servers, and now that I've switched domains, everything seems a bit messed up.
I've changed the urls and directories, and I've got my chmod set at 777 for the directories needed (cdn and img)
Script:
$folder = "/cdn/img";
$HTTP_POST_FILES = "";
if(isset($_FILES['filename']['tmp_name'])) {
if (is_uploaded_file($_FILES['filename']['tmp_name'])) {
$ext = strtolower(end(explode('.', $_FILES['filename']['name'])));
$fileCode = fileCode($ext);
if(move_uploaded_file($_FILES['filename']['tmp_name'], $folder . $fileCode)) {
echo 'Your file has been uploaded! View and share your file here';
} else {
echo "THERE'S A GLITCH IN THE MATRIX! YOUR FILE COULDN'T BE UPLOADED!";
}
} else {
Echo "Failed. Try again.";
}
And I'm getting this error:
Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpZ1TbaL' to 'http://codyleek.me/cdn/img/e6fmd6.png' in /home/codyleek/public_html/cdn/upload.php on line 17
Sorry that my formatting sucks. I'm new here aha.
But um, could any of you help me? I've tried redoing the URLs, resetting permissions, everything I can think of.
My PHP knowledge is limited.
Thanks in advance.
You should move it to a path on your disk (for example /srv/www/mydomain.com/img/test.png) and not to another website like you're doing now (http://...). By using http://x you are saying: 'use the HTTP-protocol, on domain x to ....'

Uploading a file using 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?

Categories