# FILE UPLOAD TROUBLESHOOTING GUIDE

## 🚨 Upload Not Working? Quick Fixes

### STEP 1: Run the Diagnostic Tool (2 minutes)

Visit: `https://yourdomain.com/project-planner/test_upload.php`

This will:
- ✅ Check your server configuration
- ✅ Verify upload directory exists and is writable
- ✅ Test actual file upload
- ✅ Show specific error messages
- ✅ Provide fix commands

**Delete test_upload.php after use for security!**

---

## 🔧 Common Issues & Solutions

### Issue 1: "No file uploaded" or Silent Failure

**Causes:**
- uploads/ directory doesn't exist
- uploads/ directory not writable
- Form missing enctype attribute

**Solutions:**

**A. Create/Fix uploads directory via SSH/Terminal:**
```bash
cd /path/to/project-planner/
mkdir -p uploads
chmod 755 uploads
```

**B. Via cPanel File Manager:**
1. Navigate to project-planner folder
2. Click "New Folder" → Name it "uploads"
3. Right-click uploads folder → Permissions
4. Set to 755 (Owner: rwx, Group: r-x, World: r-x)
5. Click "Change Permissions"

**C. Via FTP (FileZilla, etc.):**
1. Connect to your server
2. Navigate to project-planner folder
3. Create folder named "uploads"
4. Right-click → File Permissions → Set to 755

---

### Issue 2: "Permission denied" Error

**Cause:** Web server can't write to uploads directory

**Solutions:**

**Try permissions in this order:**

1. **755 (Recommended):**
   ```bash
   chmod 755 uploads
   ```

2. **If 755 doesn't work, try 775:**
   ```bash
   chmod 775 uploads
   ```

3. **Last resort - 777 (Less secure):**
   ```bash
   chmod 777 uploads
   ```

**Via cPanel:**
- File Manager → Right-click uploads folder → Permissions
- Check: Read, Write, Execute for Owner
- Check: Read, Execute for Group and Public
- Numeric Value: 755

**Check folder ownership:**
```bash
ls -la uploads/
# Should show web server user (often www-data, apache, or your cPanel user)
```

---

### Issue 3: "File exceeds upload limit"

**Cause:** File larger than PHP upload_max_filesize

**Check current limits:**
Create `info.php`:
```php
<?php phpinfo(); ?>
```
Visit: `yourdomain.com/project-planner/info.php`
Look for: `upload_max_filesize` and `post_max_size`
**Delete info.php after checking!**

**Solution A - Edit .htaccess:**

Add to `/project-planner/.htaccess`:
```apache
php_value upload_max_filesize 20M
php_value post_max_size 20M
php_value max_execution_time 300
php_value max_input_time 300
php_value memory_limit 128M
```

**Solution B - php.ini (if available in cPanel):**

Add/edit these lines:
```ini
upload_max_filesize = 20M
post_max_size = 20M
max_execution_time = 300
max_input_time = 300
memory_limit = 128M
```

**Solution C - Contact Hosting Provider:**

If above don't work, your host may need to increase limits server-wide.

---

### Issue 4: "File upload not enabled"

**Check if enabled:**
```php
<?php echo ini_get('file_uploads') ? 'Enabled' : 'Disabled'; ?>
```

**Solution:**

Add to `.htaccess`:
```apache
php_flag file_uploads On
```

If that doesn't work, contact your hosting provider - they need to enable it at the server level.

---

### Issue 5: Excel Files Won't Upload (CSV Works)

**Cause:** Excel parsing requires additional PHP library

**Solutions:**

**Option 1 - Use CSV format (Recommended):**
1. Open your Excel file
2. File → Save As
3. Choose "CSV (Comma delimited) (*.csv)"
4. Upload the CSV file instead

**Option 2 - Install PhpSpreadsheet (Advanced):**

If you have Composer access:
```bash
cd /path/to/project-planner/
composer require phpoffice/phpspreadsheet
```

Then update `includes/spreadsheet-parser.php` to use the library.

**For now: Just use CSV format - it's simpler and more reliable!**

---

### Issue 6: "Missing temporary folder"

**Cause:** PHP can't find/access upload_tmp_dir

**Check current setting:**
```php
<?php echo ini_get('upload_tmp_dir'); ?>
```

**Solution in .htaccess:**
```apache
php_value upload_tmp_dir /tmp
```

Or create custom tmp directory:
```bash
mkdir -p /home/your_cpanel_user/tmp
chmod 755 /home/your_cpanel_user/tmp
```

Then in .htaccess:
```apache
php_value upload_tmp_dir /home/your_cpanel_user/tmp
```

---

### Issue 7: Upload Works But Import Fails

**Possible causes:**
- CSV format not recognized
- Column headers don't match expected format
- Date format issues
- Encoding issues

**Solutions:**

**A. Verify CSV format:**
- First row should be headers
- Columns: Category, Task, Responsible, Due Date, etc.
- Download sample_workplan.csv for reference

**B. Check encoding:**
- Save CSV as UTF-8 encoding
- In Excel: Save As → Tools → Web Options → Encoding → UTF-8

**C. Date format:**
- Use YYYY-MM-DD (2026-01-15)
- Or MM/DD/YYYY (01/15/2026)
- Or DD/MM/YYYY (15/01/2026)

**D. Enable debug mode:**

In `config.php`:
```php
define('DEBUG_MODE', true);
```

Try uploading again - you'll see detailed error messages.
**Set back to false after troubleshooting!**

---

## 📋 Pre-Upload Checklist

Before uploading, verify:

- [ ] uploads/ directory exists
- [ ] uploads/ directory has 755 permissions
- [ ] uploads/.htaccess file exists (for security)
- [ ] File is under upload size limit
- [ ] File is .csv format (most reliable)
- [ ] CSV has header row with column names
- [ ] No special characters in filename

---

## 🔍 Step-by-Step Debugging

### 1. Check Server Configuration

Run test_upload.php - it will show:
- PHP version
- Upload settings
- Directory status
- Test upload capability

### 2. Test with Small File

Create a simple test.csv:
```csv
Category,Task,Responsible,Due Date
Test,Test Task,Me,2026-01-20
```

Try uploading this 1KB file. If it works, issue is file size/format.

### 3. Check PHP Error Logs

**Via cPanel:**
- Errors → Error Log
- Look for upload-related errors

**Via SSH:**
```bash
tail -f /path/to/error_log
# Or
tail -f /var/log/apache2/error.log
```

### 4. Check File Permissions

```bash
# Check uploads directory
ls -ld uploads/
# Should be: drwxr-xr-x (755)

# Check parent directory
ls -ld .
# Must allow web server to access uploads/
```

### 5. Verify .htaccess Not Blocking

Temporarily rename .htaccess to .htaccess.bak and test.
If upload works, there's a problematic rule in .htaccess.

---

## 🚀 Alternative: Manual Task Entry

If uploads continue to fail:

1. Go to project page
2. Click "Add Task" button
3. Enter tasks manually one by one
4. Or copy/paste from your spreadsheet

While slower, this guarantees your data gets entered.

---

## 💾 cPanel-Specific Instructions

### Method 1: cPanel File Manager

1. **Login to cPanel**
2. **File Manager** → Navigate to public_html/project-planner/
3. **Create folder:** Click "+ Folder" → Name: "uploads"
4. **Set permissions:**
   - Right-click uploads folder
   - Change Permissions
   - Check: Read, Write, Execute for Owner
   - Check: Read, Execute for Group & World
   - Numeric: 755
   - ✓ Click "Change Permissions"

### Method 2: cPanel PHP Settings

1. **MultiPHP INI Editor** in cPanel
2. Select your domain
3. Increase these:
   - upload_max_filesize: 20M
   - post_max_size: 20M
   - max_execution_time: 300
4. Save Changes

---

## 📞 When to Contact Hosting Support

Contact your hosting provider if:

1. ✅ You've tried all solutions above
2. ✅ test_upload.php shows "file_uploads: Disabled"
3. ✅ You can't create directories
4. ✅ Permission changes don't stick
5. ✅ You need to increase upload limits beyond .htaccess

Ask them to:
- Enable PHP file uploads
- Increase upload_max_filesize to 20M
- Set correct permissions on uploads directory
- Check server error logs

---

## 🔒 Security Notes

**After fixing upload issues:**

1. Delete diagnostic files:
   - test_upload.php
   - info.php (if you created it)
   - Any test files

2. Verify uploads/.htaccess exists with:
   ```apache
   Options -Indexes
   <FilesMatch "\.(php|phtml|php3)$">
       Order Deny,Allow
       Deny from all
   </FilesMatch>
   ```

3. Never set uploads/ to 777 permanently - use 755 or 775

---

## 📊 Quick Reference

| Error | Quick Fix |
|-------|-----------|
| No file uploaded | Create uploads/ folder, set 755 |
| Permission denied | chmod 755 uploads/ |
| File too large | Edit .htaccess: php_value upload_max_filesize 20M |
| Upload disabled | Contact host to enable file_uploads |
| Excel won't parse | Convert to CSV format |
| Missing tmp dir | Add upload_tmp_dir in .htaccess |

---

## ✅ Success Checklist

Upload working when:

- [ ] test_upload.php shows all green "OK" status
- [ ] Test file uploads successfully
- [ ] Sample CSV imports tasks
- [ ] No error messages in project creation
- [ ] Tasks appear in project after upload

---

## 🎯 Recommended Setup

**Most reliable configuration:**

1. **File format:** CSV (not Excel)
2. **Permissions:** 755 for uploads/ directory
3. **Size limit:** 10-20MB (via .htaccess)
4. **Security:** .htaccess in uploads/ folder
5. **Encoding:** UTF-8
6. **Backup:** Always keep original spreadsheet

---

**Still stuck?**

1. Run test_upload.php and note ALL error messages
2. Enable DEBUG_MODE in config.php
3. Check error_log for PHP errors
4. Try with sample_workplan.csv first
5. Consider manual task entry as fallback

**Most issues are fixed by ensuring uploads/ directory exists with proper permissions (755).**
