Jump to content

[SOLVED] Image upload


maxso

Recommended Posts

What if statement can determine whether an upload was complete from this piece of code?

 

<?
//print_r($_POST);

if($_POST["action"] == "Upload Image")
{
unset($imagename);

if(!isset($_FILES) && isset($HTTP_POST_FILES))
$_FILES = $HTTP_POST_FILES;

if(!isset($_FILES['image_file']))
$error["image_file"] = "An image was not found.";


$imagename = basename($_FILES['image_file']['name']);
//echo $imagename;

if(empty($imagename))
$error["imagename"] = "The name of the image was not found.";

if(empty($error))
{
$newimage = "images/" . $imagename;
//echo $newimage;
$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
$error["result"] = "There was an error moving the uploaded file.";
}
}

include("upload_form.php");

if(is_array($error))
{
while(list($key, $val) = each($error))
{
echo $val;
echo "<br>\n";
}
}

?>

Link to comment
Share on other sites

I tried an else statement like...

 

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(empty($result))
    $error["result"] = "There was an error moving the uploaded file.";
else{
echo "complete"
}

 

but it didnt say anything after the upload so i dont know.

Link to comment
Share on other sites

1) try this:

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(!empty($result)) {
    $error["result"] = "There was an error moving the uploaded file."
;}
else {
echo "complete";
}

 

2) try this:

 

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(!empty($result)) {
    $error["result"] = "There was an error moving the uploaded file."
;}
else {
$completemsg = "complete";
}

then put this wherever you want:

<?php echo $completemsg; ?> 

 

Link to comment
Share on other sites

It checks if they have left that box empty or not. if(result) doesnt meen anything does it? If result=what?

 

I do use this kind of logic in my forms, and it works rather well.  For instance, at the very top of a form validation, I use if ($_POST['name_of_form']) and that causes the if statement to fire if they press the submit button and the PHP_SELF is the method.  I am not sure if it works the same with regular variables, but I would not be surprised if it did.

 

For instance, $result = "blah" returns as "true", where as $result = "" returns as false.  Null, 0, and an unset variable will return as false; however, I think empty() will give you a more absolute true/false.  I am pretty sure that even if $result = 0, on a empty($result) it will return false (it has been set to something).

 

All of the above statements should be confirmed by a more experienced coder, as I am just a bumbling fool still. :)

Link to comment
Share on other sites

It checks if they have left that box empty or not. if(result) doesnt meen anything does it? If result=what?

 

If result=same code you had above.

Assigning function to a variable and just checking whether it's empty doesn't mean anything for sure.

 

 

if($result) echo 'success';

this will execute function assigned to $result and print out success.

Link to comment
Share on other sites

Thank you all who helped. Thank you to Work_it_work who solved it also.

$result = @move_uploaded_file($_FILES['image_file']['tmp_name'], $newimage);
if(!empty($result)) {
    $error["result"] = "There was an error moving the uploaded file."
;}
else {
echo "complete";
}

 

Topic solved

Link to comment
Share on other sites

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.