Jump to content

function move_uploaded_file(); is not moving the file to the assiged folder


Recommended Posts

The code belows will present an error if there is not image in t he field, else it will go ahead change the name of 

<?php $_FILES['file']['tmp_name'],  ?>

to

<?php  $newname = $id . ".jpg"; ?>

then inside the else if $place_file is not true then it will echo file not uploaded  and that's the error i am  getting now don't have any idea...

 <?php if (!$_FILES['file']['tmp_name']) { 

            $error_msg = '<font color="#FF0000">ERROR: Please browse for an image before you press submit.</font>';

        } else { 

                        $newname = $id . ".jpg";
                        $place_file = move_uploaded_file( $_FILES['file']['tmp_name'], "inventory_images/$nawname");
                        $success_msg = '<font color="#009900">Your image has been updated, it may take a few minutes for the changes to show... please                                                    be patient.</font>';

// Check to make sure the move result is true before continuing
if ($place_file != true) {
    echo "ERROR: File not uploaded. Try again.";
    unlink 
($_FILES['file']['tmp_name']); // Remove the uploaded file from the PHP temp folder
    
}  
           
} ?>

if this is not working, there would be errors in your error.log, however you mispelled you $newname variable

 

 $place_file = move_uploaded_file( $_FILES['file']['tmp_name'], "inventory_images/$newname"); //you had is spelled $nawname, not $newname

I have to give you the pro, This time doesn't present the message but when i go inside the invertory_images i don't see the image id= in this case is 180.jpg i don't see the image in there.  Even though it is not presenting any error, I guess that what was happening before, any ideas?

The id is a is equal to the Variable session of the User id

 

<?php $id = $_SESSION['user_id'];?>

 

i just echo it and it is printing the id, so it means the $id is not empty, Also when I go to the product it will show or better said dispay a empty picture but with the name of the id of the product, Which means the id it's inserting but there is not picture plus it is not in the inventory_images.

 

okay in your else block let's place some debugging in there to pin the issue to the move_uploaded_file function

 

else { 

                        $newname = $id . ".jpg";
                       if(!move_uploaded_file($_FILES['file']['tmp_name'], "inventory_images/$nawname")) print "Cannot move {$_FILES['file']['name']}";

i have placed this

 

<?php  if($place_file != true){ print("Cannot move {$_FILES['file']['name'])}";
                        $success_msg = '<font color="#009900">Your image has been updated, it may take a few minutes for the changes to show... please                                                    be patient.</font>';?>

 

there is a parse error

 

Parse error: syntax error, unexpected T_VARIABLE in on that line.

 

 

I don't have editor for blind people here lol I am a work Don't see a T_VARIABLE  error here.

 

I finally came into an editor and figure out what was happening. Ok I have some result on the debugging script you just gave me. Yes the field $_FILES['file']['name'] Can't be move, Basically the form is submitted but the picture field not...

 

 

Cannot move Ixia.gif

 

The quote above is what is printing in the browser. In other words is not getting inside the folder.

 

This is the script I am using to upload the file.

 

 

<?php

if (0 === count($errors)){


if (!$_FILES['file']['tmp_name']) { 

            $error_msg = '<font color="#FF0000">ERROR: Please browse for an image before you press submit.</font>';

        } else {

            $maxfilesize = 501200; // 51200 bytes equals 50kb
            if($_FILES['file']['size'] > $maxfilesize ) { 

                        $error_msg = '<font color="#FF0000">ERROR: Your image was too large, please try again.</font>';
                        unlink($_FILES['file']['tmp_name']); 

            } else if (!preg_match("/\.(gif|jpg|png)$/i", $_FILES['file']['name'] ) ) {

                        $error_msg = '<font color="#FF0000">ERROR: Your image was not one of the accepted formats, please try again.</font>';
                        unlink($_FILES['file']['tmp_name']); 

            } else { 

                        $newname = $id . ".jpg"; // 
                        $place_file = move_uploaded_file( $_FILES['file']['tmp_name'], "inventory_images/$newname");
                        
if(!move_uploaded_file( $_FILES['file']['tmp_name'], "inventory_images/$newname"){print "Cannot move {$_FILES['file']['name']}";
                        
// Check to make sure the move result is true before continuing
if ($place_file != true) {
    echo "ERROR: File not uploaded. Try again.";

}  
           
} ?>[code]

If there is not error the script above is set to upload or move_upload to the folder images_inventories. I am looking to name the picture or file to the user_id that's why I have tried to re-name it to  [code]<?php  $newname = $id . ".jpg" ?>

 

Nobody's said anything about permissions, so I'm just going to ask. Are you sure the 'inventory_images' directory exists in the same place as this php file and you have write permissions to it?

 

Add this to your file and post what you get:

echo "does dir exist? ";
var_dump(file_exists('./inventory_images'));
echo "<br>\nis dir writable? ";
var_dump(is_writable('./inventory_images'));
echo "<br>\n";

You can get php to tell you why the move_uploaded_file statement is failing by developing and debugging your code on a system with error_reporting set to E_ALL (or even better a -1) and display_errors set to ON. Add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(-1);

Nobody's said anything about permissions, so I'm just going to ask. Are you sure the 'inventory_images' directory exists in the same place as this php file and you have write permissions to it?

 

Add this to your file and post what you get:

echo "does dir exist? ";
var_dump(file_exists('./inventory_images'));
echo "<br>\nis dir writable? ";
var_dump(is_writable('./inventory_images'));
echo "<br>\n";

 

I thought about the permissions, thought I think I have done that. Yes there is a folder called inventory_images in the same folder where the file is.

 

This is the result for inventory_images folder

 

does dir exist? bool(true)

is dir writable? bool(true)

 

Permission for this folder are set to 0755

 

 

 

You can get php to tell you why the move_uploaded_file statement is failing by developing and debugging your code on a system with error_reporting set to E_ALL (or even better a -1) and display_errors set to ON. Add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(-1);

You can get php to tell you why the move_uploaded_file statement is failing by developing and debugging your code on a system with error_reporting set to E_ALL (or even better a -1) and display_errors set to ON. Add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(-1);

 

I have put the error_reporting script on top and there is an undefined index according to error_reporting function. it comes out as undefined after it goes through a function.

 

<?php function error_for($name){
global $errors;
if($errors[$name]){
return "<div class='form_error'>". $errors[$name]; ?>

 

This is the function which will print the errors below each field on the test for validations to see if the fields are empty or not. the undefined index will display on the screen below each form field before being submitted and after submittion with the error reporting_script above. Didn't see any error concerning the move_uploaded_file() function or the folder inventory_images. Don't know if those undefined indexes are the ones causing the problem, eventhough the INSERT query is inserting the field values in the database but the file in Mr, Inventory_images.

Your form element's opening tag looks something like this, doesn't it?

<form enctype="multipart/form-data" action="uploader.php" method="post">

 

yes the enctype is multipart/form-data.

 

like

 

<form action="formexample.php" method="post" enctype="multipart/form-data">

Here's the list I posted -

 

1) You should test if the current visitor is logged in before you do any operations on the page. You should not display the form or execute the form processing code if the visitor is not logged in.

 

2) Textareas don't have value='' attributes. You output the existing content between the <textarea></textarea> tags.

 

3) If you are not getting a large number of 'undefined' error messages and some depreciated errors, something in one or the other of your include files is disabling the error reporting/display errors settings.

 

4) You need to use isset() to test if variables exist before referencing them to avoid generating undefined variable error messages.

 

5) You are filtering your $_POST data after you have validated it. You should filter it first so that the validation will check the filtered content.

 

6) You are not filtering the price, zipcode, or category_id.

 

7) You are still using some ereg_replace functions (instead of preg_replace.)

 

8) Your preg_replace code for the $_POST fields doesn't work. It needs delimiters around the match pattern (you cannot just change the name 'ereg' to 'preg').

 

9) Your code is currently displaying the 'Cannot move imagename.ext' because you have two move_uploaded_file() statements and after the first one is executed, the tmp file no longer exists and cannot be moved again.

 

10) Your code is not using any of the uploaded image information in any of the queries.

 

11) You need to indent your code so that lines at the same block level are indented the same amount and clean up the excess white space.

 

12) You have three copies of $_SESSION['customer_id'] by different names. You should just use $_SESSION['customer_id'] where you need it or get your code under control and use just one copy of it.

 

13) Your upload error messages are being handled differently from the other error messages. You are also not displaying the $error_msg variable, so any of the upload errors aren't being displayed, so you won't know why your code is failing. Use the $errors array method for all the user error messages.

Here's the list I posted -

 

 

 

4) You need to use isset() to test if variables exist before referencing them to avoid generating undefined variable error messages.

 

 

 

The only undefined variables I had were the ones that were not defined such as $user_pic which I took that from another file code by mistake and $product that was mispelled now the real problem lay down in the indexes undefined indexes. I still on step 3 to 4

Here's the list I posted -

 

 

 

4) You need to use isset() to test if variables exist before referencing them to avoid generating undefined variable error messages.

 

 

 

The only undefined variables I had were the ones that were not defined

^^ rofl. Ok, I'm done sorry. :D

 

Undefined indexes are still variables (array)

The isset(), if I remember correctly was for the following cases -

 

Your code that tests if the visitor is logged in for setting the $toplinks variable -

if(isset($_SESSION['customer_id'])) {

 

In your error_for() function -

if(isset($errors[$name])){

 

And for each of your form field value="" attributes -

<input type="text" name="name" value="<?php echo h(isset($_POST['name']) ? $_POST['name'] : ''); ?>" />

 

BTW, for a type='file' form field, you cannot set the value. To prevent the user from needing to re-enter and re-upload the file each time you redisplay the form, you should probably save a successful file upload in a session variable and output a message next to the file box in the form that the file has already been uploaded. I would suggest something like the following -

 

In your validation section -

<?php
if($image['error'] == UPLOAD_ERR_NO_FILE){
	// no file was selected. If there is no saved file, this is an error, if there is a saved file, not an error
	if(empty($_SESSION['saved_image'])){
		$errors['file'] = "No Image was selected";
	}
} else if($image['error'] != UPLOAD_ERR_OK){
	// generically handle other upload errors. Real code would display a useful message (file was too large, upload was incomplete, a server error occurred)
	$errors['file'] = "An error occurred uploading the image - Error code: {$image['error']}";
} else {
	// the image upload was successful ($image['error'] is equal to UPLOAD_ERR_OK)
	// put your application level filtering, conditioning, and validation of the uploaded file information here... (mime type, filename, extension, filesize, image size)

	// note: if the image upload was successful, you should process it (storing it in a session variable), even if there are other validation errors, so that the user does not need to keep reselecting the image file
	// if you successfully process the uploaded image here, you should display a message near the form's file selection field telling them they don't need to reselect the file
	if(is_uploaded_file($image['tmp_name'])){
		$_SESSION['file'] = $image; // save the uploaded file information (name, mime type, filesize)
		$_SESSION['saved_image'] = file_get_contents($image['tmp_name']); // save the actual file
	}
}

 

At the point where you have successfully used the form information -

<?php
	// form processing is finished without any errors
	unset($_SESSION['file']);
	unset($_SESSION['saved_image']);

 

Your file form field -

<label align="left" for="Image">Upload Image:</label>
<input name="file" type="file" size="25" /><?php if(!empty($_SESSION['saved_image'])){ echo "* Image {{$_SESSION['file']['name']}} has already been uploaded. You may select a different Image if you want.";} ?><br />

 

 

One comment,

 

Guys I never saw a comment on whether it is ok to have a picture field in this case name="file" and the rest of the text files together in one form. so in case I wanted to bring that up, I have  done a update picture form submit alone with text fields with two form one for the picture field alone and then another for the text fields. I just want to know if it is ok to go ahead and work with one form or not.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.