Jump to content

File Upload


Scud

Recommended Posts

I am currently using a form for uploading resumes.

 

<?php

$target = "upload/";

$target = $target . basename( $_FILES['uploaded']['name']) ;

$ok=1;

 

//This is our size condition

if ($uploaded_size > 350000)

{

echo "Your file is too large.<br>";

$ok=0;

}

 

//This is our limit file type condition

if ($uploaded_type =="text/php")

{

echo "No PHP files<br>";

$ok=0;

}

 

//Here we check that $ok was not set to 0 by an error

if ($ok==0)

{

Echo "Sorry your file was not uploaded";

}

 

//If everything is ok we try to upload it

else

{

if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))

{

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";

}

else

{

echo "Sorry, there was a problem uploading your file.";

}

}

 

/* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */

 

if(sizeof($_POST)) {

$body = "";

while(list($key, $val) = each($HTTP_POST_VARS)) {

$body .= "$key: $val \n";

}

 

mail("xx@xx.com", // to

"Resume",

$body, "From:".$Email);

 

}

 

// end form processing

?>

 

I am hoping that if no file is uploaded it skips the upload process and just sends the email. Also how can i make it so when the file is uploaded it is renamed to "Resume - Users first and last name"

 

Thanks

Link to comment
Share on other sites

1. to check if a file has been uploaded..

 

if(!empty($_FILE)){
   //proceed with code
}

 

2. to change the file name, change the $target variable

 

$target = upload/whateverfilenameyouwant.jpg

 

just make sure that you get the extension right, make it dynamic based on what the extension is for $_FILE['uploaded']['name']

Link to comment
Share on other sites

I am not too experienced in Php, how can i do this so when no file is uploaded rather then running the script and stating there is an error in the upload it simply just email's the remainder of the form? However when a file has been selected it runs the upload script?

 

If an attachment is uploaded is it possible to send a link to the attachment in the email?

 

also is there any way to make the uploaded file name the name the user entered in one of the input boxes?

 

 

Link to comment
Share on other sites

<?php

if(!empty($_FILE)){
   $target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 

$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 




}
is correct just add that to the top of the script and then do a else

else {

//the email system
/* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("xx@xx.com", // to
"Resume",
$body, "From:".$Email);

}

// end form processing
}

?>

Link to comment
Share on other sites

It seems the code that Mancent has provided does not work properly, it results in a server error.

 

I have added the if empty code AyKay47 provided however still results in the error 'there was an error uploading the file'. I have implemented it as follows;

<?php
if(!empty($_FILE)){
   //proceed with code
}
else
$target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 

/* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("xx@xx.com", // to
"Resume",
$body, "From:".$Email);

}

// end form processing
?>

Link to comment
Share on other sites

Sorry the code I'm using is;

 

<?php
if(!empty($_FILE)){
   //proceed with code
}
else
{
$target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
}
/* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("xx@xx.com", // to
"Resume",
$body, "From:".$Email);

}

// end form processing
?>

Link to comment
Share on other sites

what do you get with this?

<?php
if(!empty($_FILES)){
   //proceed with code

$target = "upload/"; 
$target = $target . basename( $_FILES['uploaded']['name']) ; 
$ok=1; 

//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 

//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 

//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 

//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
} 
}
else
{
/* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */

if(sizeof($_POST)) {
$body = "";
while(list($key, $val) = each($HTTP_POST_VARS)) {
$body .= "$key: $val \n";
}

mail("xx@xx.com", // to
"Resume",
$body, "From:".$Email);

}
}
// end form processing
?>

Link to comment
Share on other sites

the blank screen means that something is happening.. more then likely you have a syntax error which is prohibiting the output to be sent.. check and make sure that you have these two settings at the top of your page..

 

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

 

this should output any present errors to your browser..

Link to comment
Share on other sites

So i have finally managed how to find how to make it work after playing around with it for a while. The final script is as follows

<?php
    $target = "upload/$firstname $lastname -  "; 
    $target = $target . basename( $_FILES['uploaded']['name']) ; 
    $ok=1; 
    
    //This is our size condition 
    if ($uploaded_size > 350000) 
    { 
        echo "Your file is too large.<br>"; 
        $ok=0; 
    } 
    
    //This is our limit file type condition 
    if ($uploaded_type =="text/php") 
    { 
        echo "No PHP files<br>"; 
        $ok=0; 
    } 
    
    //Here we check that $ok was not set to 0 by an error 
    if ($ok==0) 
    { 
        Echo "Sorry your file was not uploaded"; 
    } 

    //If everything is ok we try to upload it 
    else 
    { 
        if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
        { 
            echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 
        } 
        elseif (!empty($_FILES)){
            //proceed with code
        }
        else
        { 
            echo "Sorry, there was a problem uploading your file."; 
        } 
    } 
    
    /* this part can either be in the same file as the form or it can be in a different file. If you want this in a different file, make the "action" of the form go to that file */
    
    if(sizeof($_POST)) {
        $body = "";
        while(list($key, $val) = each($HTTP_POST_VARS)) {
            $body .= "$key: $val \n";
        }
        
        mail("xx@xx.com", // to
             "Resume",
             $body, "From:".$Email);
        
    }
    
    // end form processing
    ?>

 

Is it possible however to change the position of the echo. It simply displays at the top of the page to the left. I was hoping to fit it along nicer with my theme. How can i change this. Under my php script is a html file which displays a certain page, i was hoping i could incorporate the echo to become more a status on my page.

 

Is there anyway to also send a direct link in the email to the uploaded file?

Link to comment
Share on other sites

thanks for your help aykay.

 

How can i do both of these things?

I would like to place the echo function in a particular part of my page, what code do i insert where i would like it to be placed.

 

Similarly what code do i insert into my php script to send the direct path.

Link to comment
Share on other sites

1. wrap the code that you are outputting in an element tag like a <span>, then assign it a class and use CSS to move it wherever you like..

 

2.

 if(sizeof($_POST)) {
        $body = "";
        while(list($key, $val) = each($HTTP_POST_VARS)) {
            $body .= "$key: $val \n";
        }
        $body .= "\n http://domain.com/path/to/file.jpg";
        
        mail("xx@xx.com", // to
             "Resume",
             $body, "From:".$Email);
        
    }

 

I have not used a mailer recently with PHP so I am not really sure if the image will show up as a link or not.. I'm sure someone else on here will know or your can look into online resources for that.

Link to comment
Share on other sites

thanks for the above code. I have managed to insert a direct path into my email however it doesn't show up as a link just plain text.

 

can you please give me an example of how i would move the echo function to a different part of the page?

Link to comment
Share on other sites

  • 1 year later...
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.