You are here: PHP File Upload - image works, pdf does not « PHP Programming Language « IT news, forums, messages
PHP File Upload - image works, pdf does not

Posted by ire.kevin on 01/03/08 21:07

Hello all, first I'd like to say I've always found google groups to be
very helpful, especially considering the help around here is out of
just plane goodness.

The problem I ran into, or rather a friend I had work on this, is
uploading a PDF. We had it working perfectly fine uploading images,
and displaying them. But then someone had the idea of also being able
to upload a PDF. And for whatever reason, it doesn't like to upload
PDF's, and from what I've gathered it isn't giving any errors either.

Maybe some of you guru's could take a look at the 2 files I have shown
below, maybe something will pop out as "huh???".

As an example of how it works... http://www.mlab-ymf.org/careers/jobs/jobs.php

Unfortunately I can't give you the password for the login, it simply
allows users to access the 2nd page, form.php where the file uploader
is.

Thanks for any time or thought given to this.

----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
FILENAME: jobs.php
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------

<?

ob_start();
include '../../header.php';
$fname = 'jobs.php';

?>

<?
$reset = false;
$pw = 'password';



function cookie($name, $data) { return setcookie($name, $data, time()
+ 60 * 60 * 24 * 30, '/'); }

function cookieGet($name, $default = false) {
if (isset($_COOKIE[$name]))
return $_COOKIE[$name];
else
return $default;
}

function login($hash) {
$_SESSION['jLogin'] = $hash;
cookie('jLogin', $hash);

// echo 'Setcookie: ' . (cookie('jLogin', $hash) ? 'yes' : 'no');
// echo 'Hash: ' . $_SESSION['jLogin'];
}

function logout() { login(''); }

function loggedIn() {
global $pw;

$loginHash = cookieGet('jLogin');

if (isset($loginHash))
return $loginHash == md5($pw);

if (isset($_SESSION['jLogin']))
return $_SESSION['jLogin'] == md5($pw);

return false;
}

// Create the jobs file if it does not exist.
if (!file_exists('jobs.txt') || $reset) {
$file = new YFile('jobs');
$array = array(
'jobs' => array()
);
$file->open(true);
$file->set($array);
$file->close();
}

function get($name) {
if (isset($_GET[$name]))
if (get_magic_quotes_gpc())
return htmlspecialchars(stripslashes($_GET[$name]));
else
return htmlspecialchars($_GET[$name]);

if ($name == 'image') return $_POST['image'];
if ($name == 'pdf') return $_POST['pdf'];

if (isset($_POST[$name]))
if (get_magic_quotes_gpc())
return htmlspecialchars(stripslashes($_POST[$name]));
else
return htmlspecialchars($_POST[$name]);
return '';
}

function searchJobs() {
$s = get('search');
$found = false;
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
for($i = 0; $i < sizeof($jobs); $i++)
foreach ($jobs[$i] as $key=>$value)
if (!(strpos(strtolower($value), $s) === false)) {
// echo $key . ' = ' . $value;
echoJob($jobs[$i], $i);
$found = true;
break;
}

if (!$found)
echo 'No results.';


}

function editJob($e) {
$companyname = get('companyname');
$title = get('title');
$salary = get('salary');
$description = get('description');
$requirements = get('requirements');
$extra1 = get('extra1');
$name = get('name');
$pnumber = get('pnumber');
$email = get('email');
$site = get('site');
$address = get('address');

$file = new YFile('jobs');
$file->open(true);
$jobs = $file->get('jobs');

$job = array(
'companyname' => $companyname,
'title' => $title,
'salary' => $salary,
'description' => $description,
'requirements' => $requirements,
'extra1' => $extra1,
'name' => $name,
'pnumber' => $pnumber,
'email' => $email,
'site' => $site,
'address' => $address,
'imagename' => $jobs[$e]['imagename'],
'pdfname' => $jobs[$e]['pdfname']
);

$jobs[$e] = $job;
$file->set($jobs, 'jobs');
$file->close();
}

function deleteJob($e) {
$file = new YFile('jobs');
$file->open(true);
$jobs = $file->get('jobs');
unset($jobs[$e]);
$jobs = array_values($jobs);
$file->set($jobs, 'jobs');
$file->close();
}

function addJob() {
// echo 'test. <br />';
// print_r($_GET);
// print_r($_POST);
// print_r($_FILES);
if (isset($_FILES['image'])) {
// echo '!!!!!!!!!!!!!!!';
// echo "Name: ".$_FILES['image']['name']."";
// echo "Size: ".$_FILES['image']['size']."";
// echo "Type: ".$_FILES['image']['type']."";
copy ($_FILES['image']['tmp_name'], "jobs.images/" .
$_FILES['image']['name']);


}

if (isset($_FILES['pdf'])) {
// echo '!!!!!!!!!!!!!!!';
// echo "Name: ".$_FILES['image']['name']."";
// echo "Size: ".$_FILES['image']['size']."";
// echo "Type: ".$_FILES['image']['type']."";
copy ($_FILES['pdf']['tmp_name'], "jobs.pfds/" .
$_FILES['pdf']['name']);


}


$companyname = get('companyname');
$title = get('title');
$salary = get('salary');
$description = get('description');
$requirements = get('requirements');
$extra1 = get('extra1');

$name = get('name');
$pnumber = get('pnumber');
$email = get('email');
$site = get('site');
$address = get('address');

$file = new YFile('jobs');
$file->open(true);
$job = array(
'pdfname' => (isset($_FILES['pdf']) ? $_FILES['pdf']['name'] : ''),
'imagename' => (isset($_FILES['image']) ? $_FILES['image']
['name'] : ''),
'companyname' => $companyname,
'title' => $title,
'salary' => $salary,
'description' => $description,
'requirements' => $requirements,
'extra1' => $extra1,
'name' => $name,
'pnumber' => $pnumber,
'email' => $email,
'site' => $site,
'address' => $address,
'timestamp' => time()
);

$jobs = $file->get('jobs');
$jobs[] = $job;
$jobs = array_values($jobs);
$file->set($jobs, 'jobs');
$file->close();
}

function echoJobs() {
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
for($i = sizeof($jobs) - 1; $i >= 0; $i--) {
echo '<div class="job_micro">';
echo '<a href="' . $fname . '?mode=ind&index=' . $i . '"><h2>' .
$jobs[$i]['title']. ' at ' . $jobs[$i]['companyname'] . '</a>';
if (($i > -1 ) && loggedIn()) {
echo ' <a href="' . $fname . '?mode=edit&edit=' . $i . '">(Edit)</
a> | ';
echo ' <a href="' . $fname . '?mode=edit&edit=' . $i .
'&delete=1">(Delete)</a>';

}
echo '</h2>';



if ($jobs[$i]['imagename']) echo '<span class="company_image">' .
'<img src="jobs.images/' . $jobs[$i]['imagename'] . '" height="100px" /
>' . '</span>';
if ($jobs[$i]['pdfname']) echo '<span class="company_pdf">' . '<a
href="jobs.pdfs/' . $jobs[$i]['pdfname'] . '" />Link to PDF</a>';

if ($jobs[$i]['timestamp']) echo '<span><em>Posted: </em>' .
date('m/d/y', $jobs[$i]['timestamp']) . '</span>';
if ($jobs[$i]['companyname']) echo '<span><em>Company Name: </
em>' . $jobs[$i]['companyname'] . '</span>';

if ($jobs[$i]['title']) echo '<span><em>Job Title: </em>' .
$jobs[$i]['title'] . '</span>';

if ($jobs[$i]['site']) echo '<span><em>Site: </em><a href="' .
$jobs[$i]['site'] . '">' . $jobs[$i]['site'] . '</a></span>';
echo '</div>';

}
// echoJob($jobs[$i], $i);
if ( sizeof($jobs) == 0)
echo 'No jobs listed.';
$file->close();
}

function echoJob($j, $i = -1) {
echo '<div>';
echo '<h2>Job Info: ' . $j['title']. ' at ' . $j['companyname'];
if (($i > -1 ) && loggedIn()) {
echo ' <a href="' . $fname . '?mode=edit&edit=' . $i . '">(Edit)</
a> | ';
echo ' <a href="' . $fname . '?mode=edit&edit=' . $i .
'&delete=1">(Delete)</a>';

}
echo '</h2>';
if ($j['imagename']) echo '<span class="company_image">' . '<img
src="jobs.images/' . $j['imagename'] . '" height="75px" />' . '</
span>';
if ($j['pdfname']) echo '<span class="company_pdf">' . '<a
href="jobs.pdfs/' . $j['pdfname'] . '" />Link to PDF</a>';
if ($j['timestamp']) echo '<span><em>Posted: </em>' . date('m/d/y',
$j['timestamp']) . '</span>';

if ($j['companyname']) echo '<span><em>Company Name: </em>' .
$j['companyname'] . '</span>';
if ($j['title']) echo '<span><em>Job Title: </em>' . $j['title'] .
'</span>';
if ($j['salary']) echo '<span><em>Salary: </em>' . $j['salary'] .
'</span>';
if ($j['description']) echo '<span><em>Description: </em>' .
nl2br($j['description']) . '</span>';
if ($j['requirements']) echo '<span><em>Requirements: </em>' .
nl2br($j['requirements']) . '</span>';
if ($j['extra1']) echo '<span><em>Extra: </em>' .
nl2br($j['extra1']) . '</span>';

echo '<h2>Contact Info</h2>';
if ($j['name']) echo '<span><em>Name: </em>' . $j['name'] . '</
span>';
if ($j['pnumber']) echo '<span><em>Phone Number: </em>' .
$j['pnumber'] . '</span>';
if ($j['email']) echo '<span><em>Email: </em><a href="mailto:' .
$j['email'] . '">' . $j['email'] . '</a></span>';
if ($j['site']) echo '<span><em>Site: </em><a href="' .
$j['site'] . '">' . $j['site'] . '</a></span>';
if ($j['address']) echo '<span><em>Address: </em>' .
nl2br($j['address']) . '</span>';
echo '</div>';
}

?>

<?

class YFile {

function YFile($file) {
/* $folder = 'files/';

while (!is_dir($folder))
$folder = '../' . $folder;
*/
$this->path =/* $folder .*/ $file . '.txt';
$this->isOpen = false;
}

function open($write = false) {
if ($this->isOpen) return;

$this->handle = fopen($this->path, 'a+');
$this->write = $write;

if ($write)
flock($this->handle, LOCK_EX);

$size = filesize($this->path);
if ($size > 0)
$this->c = unserialize(fread($this->handle, $size));
else
$this->c = array();

if (!$write)
fclose($this->handle);

$this->isOpen = $write;
}

function get($section = '') {
if ($section == '')
return $this->c;

if (!isset($this->c[$section]))
$this->c[$section] = array();

return $this->c[$section];
}

function set(&$value, $section = '') {
if ($section == '')
$this->c = $value;
else
$this->c[$section] = $value;
}

function append(&$value, $section = '') {
if ($section == '')
$this->c[] = $value;
else
$this->c[$section][] = $value;
}


// Should only be called if written to
function close() {
ftruncate($this->handle, 0);
fwrite($this->handle, serialize($this->c));
flock($this->handle, LOCK_UN);
$this->isOpen = false;
}
}

?>







<div id="jobs">
<h3 class="h3_title">Job Listings</h3>
<a href="<?= $fname; ?>">List</a> | <a href="<?= $fname; ?>?
mode=search">Search</a>
<?
if (loggedIn()) {
echo ' | <a href="' . $fname . '?mode=add">Add Job</a>';
echo ' | <a href="' . $fname . '?mode=logout">Log out</a>';
} else
echo '| <a href="' . $fname . '?mode=list">Log in</a>';
?>
<br />
<?



//echo '!' . cookie)'jLogin') . '!';

if (isset($_GET['pass'])) {
login(md5($_GET['pass']));
header('location: ' . $fname . '');
}

if (isset($_POST['pass'])) {
login(md5($_POST['pass']));
header('location: ' . $fname . '');
}

//echo loggedIn() ? 'yeah' : 'no :(';
$mode = get('mode');
$allowed = array('', 'search', 'ind');

if (!loggedIn() && !in_array($mode, $allowed)) {
echo '<form action="' . $fname . '" method="get" id="login">';
echo '<label for="pass">Password: </label>';
echo '<input id="pass" name="pass" type="password" />';
echo '<input id="submit" name="submit" type="submit" value="Login"/
>';
echo '</form>';

} else {

switch($mode) {
case 'logout':

logout();
header('location: ' . $fname . '');
break;

case 'search':
if (isset($_GET['search'])) { // Process search
searchJobs();
break;
}

echo '<form id="search">';
echo ' <fieldset>';
echo ' <h2>Search Jobs</h2>';
echo ' <div>';
echo ' <label for="s-search">Search</label>';
echo ' <input class="input" id="s-search" name="search" type="text"
class="first" />';
echo ' <input name="mode" type="hidden" value="search" />';
echo ' </div>';
echo ' </fieldset>';
echo '</form>';

break;

case 'edit':
$e = get('edit'); // The job listing number

if (isset($_GET['title']) || isset($_POST['title'])) { // Process
edits
editJob($e);
echo '<div id="success">Job updated! <a href="' . $fname . '">Go
Back</a></div>';

break;
}

if (isset($_GET['delete'])) { // Delete listing
deleteJob($e);
echo '<div id="success">Job deleted! <a href="' . $fname . '">Go
Back</a></div></div>';
break;
}

$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
$job = $jobs[get('edit')];
$file->close();

$fMode='edit';

$button = 'Update Job';
include 'form.php';

break;

case 'add':
if (isset($_GET['title']) || isset($_POST['title'])) { // Add job
addJob();
echo '<div id="success">Job added! <a href="' . $fname . '">Go
Back</a></div></div>';
break;
}

$fMode = 'add';
$button = 'Add Job';
include 'form.php';

break;

case 'ind':
$ind = $_GET['index'];
$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
echoJob($jobs[$ind]);
break;

case '':
echoJobs();
break;

}

}
echo '</div>';

include '../../footer.php';

?>

----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
FILENAME: form.php
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------------------------------------------------------------------

<?
if (!isset($button)) $button = 'Post Job';
$e = get('edit');
?>
<form id="job" method="post" enctype="multipart/form-data">
<fieldset>
<h2>Job Information</h2>
<div>
<label for="s-companyname">Comany Name</label>
<input class="input" id="s-companyname" name="companyname"
type="text" class="first" value="<?= $job['companyname']; ?>" />
</div>
<div>
<label for="s-title">Job Title</label>
<input class="input" id="s-title" name="title" type="text"
value="<?= $job['title']; ?>" />
</div>
<div>
<label for="s-image">Image</label>
<input type="file" id="s-image" name="image" />

</div>
<div>
<label for="s-pdf">PDF</label>
<input type="file" id="s-pdf" name="pdf" />

</div>

<div>
<label for="s-salary">Salary</label>
<input class="input" id="s-salary" name="salary" type="text"
value="<?= $job['salary']; ?>" />
</div>
<div>
<label for="s-description">Job Description</label>
<textarea id="s-description" name="description"><?=
$job['description']; ?></textarea>
</div>
<div>
<label for="s-requirements">Requirements</label>
<textarea id="s-requirements" name="requirements"><?=
$job['requirements']; ?></textarea>
</div>
<div>
<label for="s-extra1">Extra (optional)</label>
<textarea id="s-extra1" name="extra1"><?= $job['extra1']; ?></
textarea>
</div>
</fieldset>

<fieldset>
<h2>Contact Information</h2>
<div>
<label for="s-name">Name</label>
<input class="input" id="s-name" name="name" type="text"
value="<?= $job['name']; ?>" />
</div>
<div>
<label for="s-pnumber">Phone Number</label>
<input class="input" id="s-pnumber" name="pnumber" type="text"
value="<?= $job['pnumber']; ?>" />
</div>
<div>
<label for="s-email">Email</label>
<input class="input" id="s-email" name="email" type="text"
value="<?= $job['email']; ?>" />
</div>
<div>
<label for="s-site">Website</label>
<input class="input" id="s-site" name="site" type="text"
value="<?= $job['site']; ?>" />
</div>
<div>
<label for="s-address">Address</label>
<textarea id="s-address" name="address"><?= $job['address']; ?></
textarea>
</div>
</fieldset>
<?
if (isset($e)) echo '<input type="hidden" name="edit" value="' .
$e . '" />';
if ($fMode) echo '<input type="hidden" name="mode" value="' .
$fMode . '" />';

?>
<input id="sub" type="submit" value="<?= $button; ?>" />
</div>



<?
/*include 'yfile.class.php';
include 'f.php';

$file = new YFile('jobs');

if (isset($_GET['edit']))
$e = $_GET['edit'];
else { // not editing mode, list all jobs.
echoJobs();
exit;
}


// if editing mode
if (isset($_GET['title'])) {
editJob($e);
echo '<div id="success">Job updated!</div>';
exit;
}

// if delete
if (isset($_GET['delete'])) {
deleteJob($e);
echo '<div id="success">Job deleted!</div>';
exit;
}

$file = new YFile('jobs');
$file->open(false);
$jobs = $file->get('jobs');
$job = $jobs[$e];

$file->close();

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" /
>
<title>Jobs: Admin Panel</title>
<style type="text/css">
* {
margin: 0;
padding: 0;
}

#nav {
background: #fafafa;
}

#page {
width: 400px;
margin: 0 auto;
font: 11px/1.8 Lucida Grande, Helvetical Arial, sans-serif;
padding-bottom: 40px;
}

#search {
padding-bottom: 30px;
border-bottom: 2px solid #d2d2d2;
}

h2 {
color: #000;
border-bottom: 1px solid #ccc;
}

form {
margin-bottom: 20px;
}

fieldset {
border: none;
margin-top: 15px;
}

fieldset div {
margin-top: 8px;
}

label {
color: #3578ac;
display: block;
padding-bottom: 3px;
}

.input {
width: 175px;
height: 15px;
padding: 3px;
display: block;
border: 1px solid #bbb;
}

textarea {
display: block;
width: 300px;
height: 100px;
padding: 3px;
border: 1px solid #bbb;
}

#sub {
display: block;
width: 100px;
margin-top: 20px;

}

</style>
</head>
<body>
<div id="page">
<div id="nav">
</div>

<!-- <form id="search">
<fieldset>
<h2>Search Jobs</h2>
<div>
<label for="s-search">Search</label>
<input class="input" id="s-search" name="search" type="text"
class="first" />
</div>
</fieldset>
</form>
<!-- --> */
?>

<? /* </body>
</html>*/ ?>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация