PHP: move_uploaded_file Issues. What's wrong here? - php

I am having a very difficult getting this working and I have yet to come up with a working scenario. Basically, this is a simple user-friendly admin which is supposed to upload an image. However, I can't get the upload to work. The filename is getting added to the database just fine but the image will not upload.
Here's the code as is:
function editMain($data){
array_pop($data);
$where = "main_id = {$data['main_id']}";
unset($data['main_id']);
//upload image
if ($_FILES['main_picture']['size'] > 1){ //if image deal with it
$data['main_picture'] = '/images/'.$_FILES['main_picture']['name'];
$uploadedfile = $_FILES['main_picture']['tmp_name'];
if (move_uploaded_file($uploadedfile, SITE_ROOT.$data['main_picture']))
echo "successfully uploaded {$data['main_picture']}<br />";
else
echo "failed to upload {$data['main_picture']}<br />";
}
Thanks for looking and thanks in advance for pointing me in the right direction!

SITE_ROOT needs to be a local directory, ie "C:\..." or "/home/...", it cannot be a URL structure. Change that to the local directory of where the file should be uploaded, check permissions, and you should be good to go.

I use it that way:
if(!is_dir($dir = $_SERVER['DOCUMENT_ROOT']."/fies_path"))mkdir($dir);
move_uploaded_file($_FILES['file']['tmp_name'],$src = $dir."/$file_name.ext");

Related

Laravel does not upload video file and store tmp filename in database

I'm working with Laravel 5.8 and I wanted to upload a video file.
So in the Controller, I added this code which does the uploading:
if ($request->file('prd_video')) {
$video = Request::file('prd_video');
$videoname = $video->getClientOriginalName();
$path = public_path().'/upload/video/products/';
$request->file('prd_video')->move($path, $videoname);
$findpro = Product::find($stored->prd_id);
$findpro->prd_video = $videoname;
$findpro->save();
}
Now when I test this, after minutes of loading, the file does not uploaded and store a name like /tmp/phpsMPjG1 as filename in the database.
Also there is no error appearing and the process of insertion seems to be completed successfully.
So what's going wrong here? How can I solve this issue?
edit: previous answer about version of aws-sdk-php was not right.

Image cannot be display after uploading

Previously, when I tried uploading image into the database, the image won't display. When I check the path in the db and in the folder, it is correct.
Correct path in db and folder.
And then when I tried to view the image that has been uploaded it says that I don't have the permission to view it.
I have also tried uploaded different photo extension and different photo viewer application and I still cannot view the image. Apart from that, I have tried
W3School PHP5 File Upload. Again same thing happen, I cannot view my image.
This is my code :
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location= $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"];
move_uploaded_file($_FILES["image"]["tmp_name"], $_SERVER['DOCUMENT_ROOT'] . '/ehars/photo/' . $_FILES["image"]["name"]);
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
Why can't I view my image? Is it because of the document root? Or is it something else? Please help me thank you.
UPDATED :
Based on the image below, my code (as shown above) is inside the admin folder. The reason why I would like to save my images in /ehars/photos so that, every level of user, admin admin2 and user can view the same photo that has been uploaded. If you could advice me what is the best way to do in order to achieve my objective above. Thanks again!
If your URL scheme is not "file://", you should authorized your browser.
I remember that you can't easily link CSS and image to the local machine due to security reasons.
change your code into this
if (!isset($_FILES['image']['tmp_name']))
{
echo "";
}
else
{
$file=$_FILES['image']['tmp_name'];
$location='/ehars/photo/' . $_FILES["image"]["name"]; //remove $_SERVER['DOCUMENT_ROOT']
move_uploaded_file($_FILES["image"]["tmp_name"], '/ehars/photo/' . $_FILES["image"]["name"]); // remove $_SERVER['DOCUMENT_ROOT'] .
mysql_query("INSERT INTO photo (location,emp_id) VALUES ('$location','$emp_id')");
}
why tou should change your code, because your server not gonna read windows path (c:/apache/htdocs/yourimagespath/yourimages.jpg); it should read (/images/yourimages.jpg), i asume htdocs is your root directory. and the result in your database is /ehars/photo/yourimages.jpg not c:/apache/htdoc/ehars/photo/yourimages.jpg.
hope it help you.

PHP move_uploaded_file not working into public server

I am trying to upload my php project into public server.
I made the image upload file when I create product or edit product.
It works in localhost, but when I move to public server, it is not working.
I think move_uploaded_file part does not working.
How can I change the link? or do I have to change anything?
When I see Filzilla, I can see remote site that it is '/www/eshopProject/inventory_images'.
And index file is '/www/eshopProject/storeAdmin'.
Do I have to change link like this?
I don't know how can I change the link.
Could you help me? uploading the image into public server is not working..
Is it any security issue? or something?
Please help me. Thanks.
--index.php--
$pid = mysql_insert_id();
//Place image in the folder
$newname = "$pid.jpg";
move_uploaded_file($_FILES['fileField']['tmp_name'], "../inventory_images/product_$newname");
First of all check the permissions of the directory as mentioned in come of the comments.
If you have shell access "chmod 777 target_dir" or "chmod 707 target_dir" should be sufficient.
Second try to debug it using if's and the file_exists function(http://php.net/manual/en/function.file-exists.php).
Something like this.
$uploadedFile = $_FILES['fileField']['tmp_name'];
$destination = "../inventory_images/product_$newname";
if(file_exists($uploadedFile))
{
echo "file uploaded to temp dir";
}
else
{
echo "file upload failed";
exit();
}
if(move_uploaded_file($uploadedFile, $destination))
{
echo "upload complete";
}
else
{
echo "move_uploaded_file failed";
exit();
}
You can also check your current working directory by using the FILE or DIR constants(http://php.net/manual/en/language.constants.predefined.php).
Try this.
echo __FILE__;
echo dirname(__FILE__);
echo __DIR__;
Use the copy() method. For me it worked.
copy($tmp_file, Destination) or
copy($tmp_image, IMAGE_DIRECTORY . SAM . $product_image);
Make sure you have write file permissions set to the folder you are trying to upload too.
I recommend setting the folders to "755" permissions and retry. This would make the permissions a little tighter.
This question is a bit old but i recently faced a similar issue where even with permission 777 on the upload folder it wouldn't work.
The issue was that the SELinux (https://wiki.centos.org/HowTos/SELinux) was on enforcing mode so i had to change it to permissive mode and then the upload works perfectly.
I hope this can help someone facing this issue.

PHP - upload and overwrite a file (or upload and rename it)?

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

File Upload in WordPress Template

Have posted this over at wp.stackex also:
Hi I am trying to upload a file that is contained within a form I have on a page template in WordPress.
I cannot seem to upload the file to my specified directory, whether there is a special way of doing this or my file path information is not correct.
Here is my code: ( this is currently insecure for simple testing purposes, so please ignore.)
$target_path = dirname(__FILE__).'/event-submissions/';
$target_path = $target_path . basename( $_FILES['event-image']['name']);
if( move_uploaded_file($_FILES['event-image']['tmp_name'], $target_path) ) {
echo "The file ". basename( $_FILES['event-image']['name']). " has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
exit;
}
$target_path displays /homepages/4/d335638566/htdocs/dev/assets/themes/momentmag/event-submissions/FILENAME.FILE_EXT
So obviously I get the message "There was an error uploading the file, please try again!"
If anyone can shed any light on this - great!
Thanks
Your code looks fine to me. It looks like there is something you've overlooked that would help, but sadly you have no idea what it is. A rule of coding that I have is that, If something that you know should work, doesn't, it means that you've overlooked something - yet you've no idea what it is.
All I can say is that you can copy the file uploading code that the P2 Reloaded theme uses. It's more superior to yours, and it makes uploaded files actually attach themselves onto a blog post.

Categories