Jump to content

Recommended Posts

hye guys,i have some problem regarding this command -> move_uploaded_file(). it seems that the image uploaded to server (tmp folder) can't be moved to upload/. below is my command:

 

<script type="text/javascript">

function show_alert()

{

var msg = "Invalid File!";

alert(msg);

window.history.back();

}

</script>

 

<script type="text/javascript">

function show_alert2()

{

var msg2 = "File already exist!";

alert(msg2);

window.history.back();

}

</script>

 

<script type="text/javascript">

function show_alert3()

{

var msg3 = "File Sucessfully moved!";

alert(msg3);

// window.history.back();

}

</script>

 

<script type="text/javascript">

function show_alert4()

{

var msg4 = "File NOT Sucessfully moved!";

alert(msg4);

// window.history.back();

}

</script>

 

<?php

$allowedExts = array("jpg", "jpeg", "gif", "png");

$extension = end(explode(".", $_FILES["file"]["name"]));

if ((($_FILES["file"]["type"] == "image/jpg")

|| ($_FILES["file"]["type"] == "image/jpeg")

|| ($_FILES["file"]["type"] == "image/gif")

|| ($_FILES["file"]["type"] == "image/png"))

&& ($_FILES["file"]["size"] > 10)

&& in_array($extension, $allowedExts))

{

if ($_FILES["file"]["error"] > 0)

{

echo "Error: " . $_FILES["file"]["error"] . "<br>";

}

else

{

echo "Upload: " . $_FILES["file"]["name"] . "<br>";

echo "Type: " . $_FILES["file"]["type"] . "<br>";

echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";

echo "Stored in: " . $_FILES["file"]["tmp_name"] . "<br>";

 

 

if (file_exists( "upload/" . $_FILES["file"]["name"]))

{

 

echo '<script type="text/javascript"> show_alert2(); </script>';

 

}

else

{

$uploaded_dir = "upload/";

$filename = $_FILES["file"]["name"];

$path = $uploaded_dir . $filename;

move_uploaded_file($_FILES["file"]["tmp_name"], $path);

 

// move_uploaded_file( $_FILES["file"]["tmp_name"] , "upload/" . $_FILES["file"]["name"] );

 

if ($moved) //check if the file were moved or not

{

echo '<script type="text/javascript"> show_alert3(); </script>';

}

 

else

{

echo '<script type="text/javascript"> show_alert4(); </script>';

}

 

// echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

 

}

}

}

else

{

echo '<script type="text/javascript"> show_alert(); </script>';

}

?>

 

here is the html command:

 

<html>

<body>

<form action="complete_upload_file_w_restriction_w_javascript_alert.php" method="post"

enctype="multipart/form-data">

<label for="file">Filename:</label>

<input type="file" name="file" id="file"><br>

<input type="submit" name="submit" value="Submit">

</form>

 

</body>

</html>

 

Please help me with this guys. thanks alot.

Link to comment
https://forums.phpfreaks.com/topic/273748-move_uploaded_file-problem/
Share on other sites

it seems that the image uploaded to server (tmp folder) can't be moved to upload/.

Why do you say that? Are you getting any error messages? If not then make sure your php.ini has

display_errors = on
error_reporting = -1

(restart the server if you have to change it), and try again.

Why do you say that? Are you getting any error messages? If not then make sure your php.ini has

display_errors = on
error_reporting = -1

(restart the server if you have to change it), and try again.

 

if u look in my code, i put if ($moved) to see whether the file from the tmp folder moved to "upload" folder. but it always return false which means not successfullt moved. how?

You've not actually saved the return value of move_uploaded_file().

 

Change this:

move_uploaded_file($_FILES["file"]["tmp_name"], $path);

To this:

$moved = move_uploaded_file($_FILES["file"]["tmp_name"], $path);

 

You need to do what requinix said because had you turned on error reporting, it probably would have give you an error of 'Undefined variable $moved'.

 

Hope this helps,

 

Regards,

 

L2c.

You've not actually saved the return value of move_uploaded_file().

 

Change this:

move_uploaded_file($_FILES["file"]["tmp_name"], $path);

To this:

$moved = move_uploaded_file($_FILES["file"]["tmp_name"], $path);

 

You need to do what requinix said because had you turned on error reporting, it probably would have give you an error of 'Undefined variable $moved'.

 

Hope this helps,

 

Regards,

 

L2c.

 

ok thank you so much. where can i check the uploaded data? can i download it back?

if u look in my code, i put if ($moved) to see whether the file from the tmp folder moved to "upload" folder. but it always return false which means not successfullt moved. how?

 

i got this in my php.ini

 

; display_errors

; Default Value: On

; Development Value: On

; Production Value: Off

 

; error_reporting

; Default Value: E_ALL & ~E_NOTICE

; Development Value: E_ALL | E_STRICT

; Production Value: E_ALL & ~E_DEPRECATED

do i need to change anything?

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.